Skip to content
LocalOnly

JSON Escape

Stable

Escape any string so it can be safely embedded inside JSON.

Everything is processed locally in your browser

About JSON Escape

The JSON Escape tool converts raw text into a form that can be safely placed inside a JSON string value. It escapes double quotes, backslashes, newlines, tabs, and other control characters according to the JSON specification. Use it whenever you need to embed arbitrary text, code, or logs inside JSON without breaking the syntax.

Features

  • Escapes double quotes, backslashes, and forward slashes as needed
  • Converts newlines, tabs, and carriage returns to \n, \t, and \r
  • Escapes control characters using \u sequences per the spec
  • Optionally wraps the result in surrounding double quotes
  • Handles multi-line input in a single pass
  • Copy the escaped string straight into your code or payload

How to use JSON Escape

  1. 1

    Paste raw text

    Add the string, code, or multi-line text you need to embed in JSON.

  2. 2

    Escape

    The tool replaces every character that would break a JSON string with its escape sequence.

  3. 3

    Choose quoting

    Decide whether to include the wrapping double quotes around the result.

  4. 4

    Copy the escaped value

    Paste it directly into a JSON string field without syntax errors.

Examples

Escaping quotes and a newline

Input

She said "hello"
and left.

Output

She said \"hello\"\nand left.

How JSON string escaping works

Only three things genuinely have to be escaped

The JSON string grammar is narrow about what it forbids inside quotes. A double quote would end the string early, so it must become `\"`. A backslash starts an escape sequence, so a literal one must become `\\`. And any character below U+0020 - the C0 control characters, including tab, newline and carriage return - is not permitted raw and must be escaped. That is the entire mandatory list.

Everything else may appear literally. Single quotes need no escaping, because JSON strings are always double-quoted. Forward slashes need none either, although `\/` is legal and you will sometimes see it, a habit inherited from embedding JSON inside HTML `<script>` blocks where the sequence `</` could terminate the tag early. Accented letters, CJK characters and emoji are all valid raw UTF-8 in a JSON string.

The five control characters that have short forms - `\b`, `\f`, `\n`, `\r`, `\t` - are conveniences. Any of them could equally be written as a `\u` escape, and the remaining control characters have to be.

Escaping a string is not the same as stringifying a document

This distinction is the source of most confusion in this area. Escaping takes the *contents* of a string and makes those contents safe to sit between quotes. It does not add the surrounding quotes. Stringifying takes an entire JSON value and turns it into a string literal, quotes included, ready to be a value inside another JSON document.

So if you paste `{"a": 1}` into an escaper you get `{\"a\": 1}` - a fragment you would place inside quotes yourself. Paste the same thing into the JSON Stringify tool and you get `"{\"a\": 1}"`, which is a complete, valid JSON string value.

Which one you want depends on what you are assembling. If you are building a string by hand in a code editor and already typed the opening quote, you want escaping. If you are nesting a whole payload as a field inside another payload, you want stringifying.

Unicode, \u escapes and the surrogate pair trap

JSON's `\u` escape carries exactly four hex digits, which addresses only the first 65,536 code points. Anything above that - most emoji, historic scripts, many mathematical symbols - cannot be written as a single `\u` escape. It has to be encoded as a surrogate pair: two `\u` escapes that a parser recombines into one character.

This is why a rocket emoji becomes `\ud83d\ude80` rather than a single escape. The two halves are meaningless alone and must stay adjacent and in order. If a pipeline truncates a string by character count, it can cut between the halves and leave an unpaired surrogate, which some parsers reject outright and others turn into a replacement character.

The practical advice is to leave non-ASCII characters unescaped wherever the transport is UTF-8-clean, which today is nearly everywhere. Escaping them is only worth doing when you have a genuinely ASCII-only channel to survive, such as an old email header or a legacy system that mangles high bytes.

Reference

JSON string escape sequences

The complete set. Everything not listed here may appear literally inside a JSON string.

CharacterEscapeRequired?
Double quote "\"Yes - it would terminate the string
Backslash \\\Yes - it starts an escape sequence
Newline (U+000A)\nYes - control characters cannot be raw
Carriage return (U+000D)\rYes
Tab (U+0009)\tYes
Backspace (U+0008)\bYes
Form feed (U+000C)\fYes
Other control chars\u0000-\u001FYes - no short form exists
Forward slash /\/No - legal but optional
Single quote '(none)No - JSON strings use double quotes
Emoji, CJK, accents\uXXXX\uXXXXNo - raw UTF-8 is valid

Which tool should you use?

These tasks overlap. Here is how to pick the right one for what you are actually doing.

You are pasting text into a string literal you are typing yourself
Escaping is what you want. It prepares the contents without adding quotes you have already written.
You need a whole JSON document as a value inside another document
Use the JSON Stringify tool. It adds the surrounding quotes and produces a complete string value.
You are reading an escaped string from a log and want the original
The JSON Unescape tool reverses this operation.
You need to embed binary data
Escaping cannot help - JSON strings hold text, not bytes. Base64 Encode it first, then store the resulting ASCII.

Use cases

  • Embedding a code snippet or SQL query inside a JSON string
  • Placing multi-line log text into a JSON field
  • Building JSON payloads by hand in tests or scripts
  • Storing HTML or markup as a JSON string value
  • Preparing text with quotes for a JSON configuration file

Troubleshooting common errors

The escaped output still breaks the JSON it is pasted into

Why: You needed stringifying rather than escaping, so the surrounding quotes are missing.

Fix: Either add the quotes yourself, or use the JSON Stringify tool, which includes them.

Backslashes have multiplied to four or eight

Why: The text was escaped more than once. Each pass doubles every backslash, and the growth is exponential.

Fix: Unescape repeatedly until the count stops falling, then escape exactly once. Escaping is not idempotent - running it twice is a real bug.

An emoji became two odd-looking \ud8xx escapes

Why: Correct behaviour. Characters above U+FFFF require a surrogate pair, because \u carries only four hex digits.

Fix: Nothing to fix. Keep the two escapes together and adjacent - a parser recombines them into the original character.

A newline in the source became a literal \n and now displays wrongly

Why: That is the required encoding. A raw newline inside a JSON string is invalid, so it must be escaped.

Fix: The consuming parser converts `\n` back to a real newline. If you are seeing `\n` in your output, the string is being displayed rather than parsed.

Limitations

What this tool deliberately does not do, so you know when to reach for something else.

  • Surrounding quotes are not added - use the JSON Stringify tool if you need a complete string value.
  • The operation is not idempotent. Escaping twice doubles every backslash and corrupts the text.
  • Binary data cannot be escaped into a JSON string; Base64-encode it first.
  • Unpaired surrogates in the input cannot be repaired, only passed through.
  • Optional escaping of forward slashes and non-ASCII characters is not applied, since neither is required by the specification.

Frequently asked questions

Learn more

Command Palette

Search for a tool or command