Strings and escaping in JSON
A JSON string is text between double quotes. Most characters can appear in it as they are, including any Unicode character like é, ß or 日本. Three kinds must be escaped with a backslash: the double quote, the backslash itself, and the control characters U+0000 through U+001F, which include the line break and the tab.
{
"quote": "She said \"hello\"",
"path": "C:\\Users\\ada",
"lines": "first line\nsecond line",
"accent": "caf\u00e9"
}Every escape JSON has
| Escape | Means |
|---|---|
\" | Double quote |
\\ | Backslash |
\/ | Slash (optional: a plain / is fine too) |
\n | Line feed, a new line |
\r | Carriage return |
\t | Tab |
\b | Backspace |
\f | Form feed |
\u00e9 | Any character by its code point, as four hex digits |
Any other backslash sequence, like \' or \x41, is invalid JSON. A single quote needs no escape, since it can't end a string.
Characters outside the basic range
A \u escape holds four hex digits, so it covers U+0000 through U+FFFF. Characters above that, like most emoji, are written as two escapes, a surrogate pair: 😀 (U+1F600) is \ud83d\ude00. You can also write the character itself, since JSON text is UTF-8.
JSON inside a string
APIs, logs and databases sometimes store JSON as text inside another JSON value. Every quote in it is then escaped: "{\"id\": 1}" is a string, not an object. To use the data, parse it once to get the string, and again to get the object.
Raw line breaks and tabs inside a string are a common mistake. See line breaks in strings.