Unescaped characters in a JSON string
A literal newline, tab or quote inside a JSON string is a parse error. Here is what has to be escaped and how to fix a document that is not.
Open the JSON formatterWhat the error looks like
Different engines word this differently. You may see any of these:
Why it happens
- A JSON string may not contain a raw control character. Newlines, carriage returns and tabs have to be written as \n, \r and \t.
- A double quote inside a string has to be escaped as \", and a backslash as \\.
- Only a fixed set of escapes is valid: \" \\ \/ \b \f \n \r \t and \uXXXX. Anything else, such as \x41 or \', is a parse error.
- The usual cause is building JSON by string concatenation instead of serialising a value.
Example
{
"note": "line one
line two",
"path": "C:\Users\me"
}
{
"note": "line one\nline two",
"path": "C:\\Users\\me"
}
How this tool reports it
The formatter reports the offending string and offers to re-escape it, preserving the text you meant rather than deleting the characters it could not read.
Paste your JSON into the formatter to see the exact line and column, or use the validator if you only need to check it.
Worth knowing
- Never build JSON by concatenating strings. Use your language's serialiser, which escapes correctly for free.
- Windows paths are the classic case: each backslash must be doubled.