How do you escape a string for JSON?
Put a backslash in front of every double quote and every backslash, and replace control characters such as line breaks and tabs with escape codes like \n and \t. The result can sit safely between the quotes of a JSON string value. This tool does that in Escape mode, and Unescape mode reverses it, turning line1\nline2 back into two lines of text.
The rules come from section 7 of RFC 8259, the JSON specification, and they're short: the double quote, the backslash and the 32 control characters from U+0000 to U+001F must be escaped. Nothing else has to be.
The escape sequences
| Character | Escaped form | Produced by Escape mode? |
|---|---|---|
" |
\" |
Yes |
\ |
\\ |
Yes |
| Line feed | \n |
Yes |
| Carriage return | \r |
Yes |
| Tab | \t |
Yes |
| Backspace, form feed | \b, \f |
Yes |
| Other control characters | \u0000 to \u001f |
Yes, e.g. \u0007 for the bell character |
/ |
\/ |
No, but Unescape accepts it |
| Any character | \uXXXX |
Only for control characters and broken surrogate halves; Unescape accepts any |
Escape mode uses the same rules as JavaScript's JSON.stringify, minus the outer quotes. Characters outside ASCII are left alone, so café 👍 comes out unchanged.
Escaping text to put in JSON
- Leave Direction on Escape.
- Paste the raw text, including any quotes, backslashes or line breaks.
- Click Convert.
- Paste the output between the quotes of your JSON value.
Three typical inputs and what you get back:
| Raw text | Escaped |
|---|---|
She said "hi" |
She said \"hi\" |
C:\Users\me |
C:\\Users\\me |
Line 1 and Line 2 on separate lines |
Line 1\nLine 2 |
The second row is the one that bites most often. Windows paths, regular expressions and LaTeX all contain backslashes, and each one has to be doubled. A JSON snippet such as {"path": "C:\Users\me"} is invalid, and a parser will reject it at the \U.
Hand-built JSON is where this matters: request bodies for curl, test fixtures, config files, or a value copied into a database column. If you're building JSON in code, let the language's serializer do the escaping instead.
Unescaping a string from a log or API response
Logs and API errors often show JSON strings in their escaped form, with \n instead of line breaks and \" around quoted words. To read one:
- Switch Direction to Unescape.
- Paste the text that sat between the quotes, without the quotes themselves.
- Click Convert.
{\"name\": \"Ann\"} unescapes to {"name": "Ann"}, which is handy when one JSON document has been stored as a string inside another. Unicode escapes are decoded as well: Price: \u20AC5 becomes Price: €5, and a surrogate pair such as \ud83d\udc4d becomes the emoji it encodes.
When unescaping fails
Unescape mode wraps your input in quotes and parses it as a JSON string, so anything a JSON parser would reject gives "Enter a valid JSON-escaped string (without the surrounding quotes)." The usual causes:
- Surrounding quotes included.
"hello"becomes""hello""internally, which isn't valid. Delete the outer quotes. - A bare double quote.
bad "quotehas an unescaped"in the middle. In valid escaped text every quote is written\". - A lone or unknown backslash escape.
C:\Users,\x41and\'all fail. JSON has no\xor\'escapes, even though JavaScript and Python do. - Real line breaks or tabs. JSON strings can't contain raw control characters, so text pasted across several lines, or containing an actual tab, is rejected. In escaped text they should already appear as
\nand\t.
If you need to check an entire document rather than one string, paste it into the JSON validator, which reports where parsing stops.
Related tools
Once your string is escaped, the JSON formatter lays out the full document for reading, and the JSON editor lets you edit it with syntax highlighting. If the text is going into a web page rather than JSON, the escaping rules are different, so use the HTML encoder instead.
Last updated