JSON escape and unescape

Inside a JSON string, double quotes, backslashes and control characters such as newlines must be escaped with a backslash. Paste text to get a string you can drop between quotes in JSON, or paste an escaped string to get the original text back.

For é, ₹ and emoji. Characters above U+FFFF become a surrogate pair of two \u escapes, as RFC 8259 specifies.

JSON allows \/ but never requires it.

Gives a complete JSON string value instead of the part that goes between the quotes.

Escaped text (goes between the quotes)

Raj said \"ship it\"\nFolder: C:\\Projects\\Shop\n\tPrice: ₹250 — paid
Characters escaped
7 of 63quotes ×2, backslashes ×2, line breaks ×2, tabs ×1
Length
63 → 70 characters

How does JSON string escaping work?

RFC 8259, the JSON standard, says every Unicode character may appear inside a JSON string except three kinds, which must be escaped: the double quote, the backslash, and the control characters U+0000 to U+001F. A string starts and ends with a double quote, so an unescaped one would end it early, and the backslash is what starts an escape.

Seven characters have two-character escapes: \" for the quote, \\ for the backslash, and \b, \f, \n, \r and \t for backspace, form feed, line feed, carriage return and tab. Every other control character is written as \u and four hex digits, so U+001F is \u001f. That is exactly what this tool writes by default, and what JavaScript's JSON.stringify writes.

Any other character may be escaped too, but doesn't have to be. A character above U+FFFF is escaped as two \u escapes, the UTF-16 surrogate pair: RFC 8259's example is the G clef U+1D11E, written \ud834\udd1e. The option to escape everything outside printable ASCII is useful when the JSON passes through a system that mangles UTF-8. The forward slash may be written \/, which RFC 8259 allows but doesn't require.

Unescaping reads the text as the inside of a JSON string, with or without its surrounding quotes. It stops at the first thing JSON doesn't allow and says where it is: an escape JSON doesn't have, such as \' or \x41 from JavaScript, \u without four hex digits, an unescaped quote, or a raw line break that should have been \n.

"  →  \"        \  →  \\        /  →  \/  (optional)
backspace \b   form feed \f   line feed \n   carriage return \r   tab \t
other U+0000–U+001F  →  \u0000 … \u001f
U+1D11E  →  \ud834\udd1e  (surrogate pair, optional)

Which characters must be escaped in a JSON string?

The 34 characters RFC 8259 requires to be escaped, then characters that may be escaped, with the escape this tool writes
Code pointCharacterEscapeRequired?
U+0022" quotation mark\"Yes
U+005C\ reverse solidus (backslash)\\Yes
U+0000control character\u0000Yes
U+0001control character\u0001Yes
U+0002control character\u0002Yes
U+0003control character\u0003Yes
U+0004control character\u0004Yes
U+0005control character\u0005Yes
U+0006control character\u0006Yes
U+0007control character\u0007Yes
U+0008backspace\bYes
U+0009tab\tYes
U+000Aline feed (newline)\nYes
U+000Bcontrol character\u000bYes
U+000Cform feed\fYes
U+000Dcarriage return\rYes
U+000Econtrol character\u000eYes
U+000Fcontrol character\u000fYes
U+0010control character\u0010Yes
U+0011control character\u0011Yes
U+0012control character\u0012Yes
U+0013control character\u0013Yes
U+0014control character\u0014Yes
U+0015control character\u0015Yes
U+0016control character\u0016Yes
U+0017control character\u0017Yes
U+0018control character\u0018Yes
U+0019control character\u0019Yes
U+001Acontrol character\u001aYes
U+001Bcontrol character\u001bYes
U+001Ccontrol character\u001cYes
U+001Dcontrol character\u001dYes
U+001Econtrol character\u001eYes
U+001Fcontrol character\u001fYes
U+002F/ solidus (forward slash)\/No, optional
U+007Fdelete (a control character JSON doesn't require escaping)\u007fNo, optional
U+00E9é\u00e9No, optional
U+20B9₹\u20b9No, optional
U+2028line separator\u2028No, optional
U+1D11E𝄞 G clef\ud834\udd1eNo, optional

How to escape or unescape a JSON string

  1. 1
    Choose escape or unescapeEscape turns text into something you can put between the quotes of a JSON string; unescape turns \n, \" and \u escapes back into the characters.
  2. 2
    Paste your textPaste the text, or the escaped string with or without its quotes. The result updates as you type, and nothing is sent anywhere.
  3. 3
    Pick the optionsWhen escaping, choose whether to write non-ASCII characters as \u escapes, whether to escape forward slashes, and whether to wrap the result in quotes.
  4. 4
    Copy the resultCopy the output. If unescaping fails, the error names the escape that isn't valid JSON and which character it is.

Frequently asked questions

Which characters have to be escaped in a JSON string?
Only three kinds, according to RFC 8259: the double quote (\"), the backslash (\\) and the control characters U+0000 to U+001F, such as the line feed (\n) and tab (\t). Everything else, including é, ₹, emoji and the single quote, can be written as it is.
How do I put a line break inside a JSON string?
Write it as \n (and \r\n for a Windows line ending). A real line break typed inside the quotes is a control character, which JSON doesn't allow unescaped, so JSON.parse rejects the whole document. Escape the text here and the line breaks become \n.
Why is \' an invalid escape in JSON?
JSON strings are always in double quotes, so the single quote never needs escaping, and RFC 8259 doesn't define \'. The only escapes are \", \\, \/, \b, \f, \n, \r, \t and \u with four hex digits; JavaScript extras such as \', \x41, \0 and \u{1F600} make JSON.parse fail.
How is a character above U+FFFF written as a \u escape?
As two \u escapes, the UTF-16 surrogate pair, because \u takes only four hex digits. RFC 8259's example is the G clef, U+1D11E, written \ud834\udd1e; the grinning face emoji U+1F600 is \ud83d\ude00. Most JSON doesn't need this, since the character can be written directly in UTF-8.
Is every JSON string a valid JavaScript string?
Since ES2019, yes. Before that, JSON allowed the characters U+2028 (line separator) and U+2029 (paragraph separator) unescaped inside strings, but JavaScript string literals didn't, so pasting such JSON into older JavaScript source was a syntax error. TC39's JSON superset proposal removed the difference.

Sources

Related tools

See all 73 free tools

Need a custom dev tool for your team?

We build internal tools, dashboards, APIs, and SaaS platforms. If you can describe it, we can ship it.

Built by CountryDevs · No signup · No ads · We count visits, never what you enter