Escape and unescape JSON strings: quotes, backslashes and line breaks

By TechCompare · Updated · How we check

Escape turns text into a JSON string by wrapping it in quotes and writing each quote, backslash, line break and tab as a short escape. Unescape reverses that, and it takes the string with or without its outer quotes.

You have text with quotes or line breaks that has to sit inside a JSON string, or an escaped string from a log or an API that you need to read. This page covers both directions in the tool, the exact escape rules from RFC 8259, and the mistakes that break a string when it's pasted back and forth.

Topic
Escape JSON strings
json-escape
Category
Troubleshooting
JSON knowledge base

How this is calculated

Here's what the tool gives for a short sample. It holds some quotes, a line break, a file path and a tab, and Escape mode turns the whole thing into one line.

Text
He said "hi"
C:\temp\new	done
As a JSON string
"He said \"hi\"\nC:\\temp\\new\tdone"

Read the escaped line one escape at a time. Each quote in "hi" becomes \". The line break becomes \n, which is a backslash and the letter n. The tab becomes \t. The file path has two backslashes, and each one doubles, so C:\temp\new is written C:\\temp\\new. The letters after those backslashes are plain text, so the \\t in the output is an escaped backslash followed by a plain t, not a tab.

In JavaScript, JSON.stringify of a string value gives this same quoted, escaped text. The rules are in RFC 8259 section 7, and they say:

  • The quotation mark becomes \", and the backslash becomes \\. Both must be escaped.
  • Control characters from U+0000 to U+001F must be escaped too. Five have short forms: \b for backspace, \f for form feed, \n for line feed, \r for carriage return and \t for tab. Any other control character uses \u and four hex digits, like \u0001.
  • When you escape a character above U+FFFF, it becomes a surrogate pair of two \u escapes. U+1D11E becomes \uD834\uDD1E.
  • A forward slash can be written \/, but RFC 8259 never requires it, and the tool leaves / alone.
  • Other characters, like é and 😀, can stay as they are. RFC 8259 section 8.1 requires UTF-8 for JSON text exchanged outside a closed ecosystem, so no escape is needed.

Unescape works the other way. It takes the string with or without its outer quotes, so "{\"a\":[1,2]}" and {\"a\":[1,2]} both give {"a":[1,2]}. When the result looks like JSON, the Format it button formats it.

JSON inside a string
{"payload":"{\"a\":[1,2]}"}

A JSON document can hold another one as a string, which is the common reason to unescape. Here the payload value is a string, and its text is {"a":[1,2]}. A parser returns that text, not an object, so unescape the value with its quotes to get the inner document. Watch for four mistakes, and start with the first if your text came from a log or an API response, since it may already hold escapes:

  • Double escaping. Escape doubles every backslash, so text that's already escaped, like a line from a log, gets escaped again. If the output shows \\\" where you expected \", the text went through Escape twice. Unescape text that already has escapes instead of escaping it.
  • A raw line break. A control character can't sit unescaped inside a string, so a string that spans two lines fails. Unescape reports a "Bad control character" error and says a line break has to be written as \n. Replace the break with a backslash and an n, or run the text through Escape, which writes the \n for you.
  • JavaScript escapes. \x41 and \' are valid in a JavaScript string literal, but JSON has neither. Unescape rejects both, because it runs JSON.parse. Write \u0041 for A, and drop the backslash before a single quote, since ' needs no escape in a JSON string.
  • Python's default. json.dumps("é😀/") gives "\u00e9\ud83d\ude00/". The tool gives "é😀/" for the same input, and both mean the same text. The Python json docs say: "If True (the default), the output is guaranteed to have all incoming non-ASCII and non-printable characters escaped." Pass ensure_ascii=False to keep the characters.

Verdict

Let the tool write the escapes, then run the result through Unescape before it goes anywhere. A single bad backslash breaks a hand-typed string, so paste the text instead of retyping it. If the system on the other end only takes ASCII, the tool's output won't fit, since it keeps characters like é and 😀. Everything runs in your browser, and the text stays out of the page address.

More JSON scenarios

Frequently asked questions

How do I escape a JSON string?
Open Escape mode and paste the text. The result comes wrapped in outer quotes, and you can leave them off when you're pasting into a string that already has its own. Quotes, backslashes, line breaks and tabs all get escapes.
How do I unescape a JSON string?
Paste the string into Unescape. The outer quotes are optional, so {\"a\":[1,2]} and "{\"a\":[1,2]}" both give {"a":[1,2]}. If the result looks like JSON, the Format it button formats it.
Do I need to escape forward slashes in JSON?
No. RFC 8259 allows \/ but never requires it. JSON.parse turns \/ into /, and the tool leaves / alone.
Why does Python write \u00e9 when this tool writes é?
Python's json.dumps escapes non-ASCII characters by default, so é becomes \u00e9. This tool keeps the character. Both mean the same text, and passing ensure_ascii=False to json.dumps keeps the character in Python too.
Why does my string say Bad control character or Bad escaped character?
A raw line break gives an error like "Bad control character", and a backslash before something JSON doesn't allow, like \x41 or \', gives an error like "Bad escaped character". Replace the break with \n and the bad escape with \u0041 or the plain character. The exact words come from the JSON parser in your browser, so they can differ from one browser to another.