About escaping JSON strings
Why JSON strings need escaping
A JSON string sits between two double quotes. Some characters cannot appear in it as they are. RFC 8259 (section 7) says a string must escape the double quote, the backslash and the control characters U+0000 to U+001F, which include newline and tab. The short escapes are \", \\, \/, \b, \f, \n, \r and \t. Any other character can be written as \u plus four hex digits.
You need this whenever text goes inside a JSON value by hand: a multi-line SQL query or shell script in a config file, a prompt in an API request, or a whole JSON document stored as a string field, which is common in message queues, webhooks and log pipelines.
What this tool changes
Escape does what RFC 8259 asks: each double quote becomes \", each backslash \\, line breaks and tabs \n, \r and \t, and any other control character a \u escape. The output matches what JSON.stringify puts between its quotes. It has no quotes around it, so paste it between the quotes of the value you are writing. For example, a line say "hi" followed by a second line becomes say \"hi\"\n plus the second line.
Unescape reverses every JSON escape, including \\, \/ and \u00e9, reading left to right so an escaped backslash followed by n stays a backslash and an n. It is handy for a log line or an API error that shows a payload full of backslashes. The output updates as you type, and everything runs in your browser.
Backslashes and Windows paths
Backslashes are the usual trap when escaping by hand. A Windows path such as C:\Users\derek must become C:\\Users\\derek: left single, a JSON parser rejects \U as an unknown escape, and a path like C:\new is silently read as a line break. This tool doubles them for you, and a Windows line break (CR LF) is kept as \r\n, so escaping and unescaping gives back exactly what you started with.
Double escaping
When a JSON string is escaped twice, quotes show up as \\\" and line breaks as \\n. This happens when a service stores a JSON document as a string and another layer escapes it again. Each layer needs one decode: press Unescape, copy the output back into the input, and unescape again until the quotes are plain. The same conversion is also available as JSON String Encode/Decode, laid out like the other encode and decode tools on this site.
Frequently asked questions
- Which characters must be escaped in a JSON string?
- The double quote, the backslash and the control characters U+0000 to U+001F (such as newline, carriage return and tab). The forward slash may be escaped but does not have to be. Non-ASCII text such as accents or Chinese can stay as it is in UTF-8.
- Why are there backslashes before every quote in my log?
- The JSON was stored as a string inside another JSON value, so its quotes were escaped. Unescape it once to get the inner document back. If you still see backslashes, it was escaped twice.
- How do I escape a string for JSON in JavaScript?
- Call JSON.stringify on the string. It adds the surrounding quotes and escapes quotes, backslashes and control characters. JSON.parse on that quoted string gives the original text back.