How browsers word it
| Chrome and Node (V8) | Bad control character in string literal in JSON at position 12 (line 1 column 13) |
|---|---|
| Chrome and Node, older | Unexpected token
in JSON at position 12 |
| Firefox | JSON.parse: bad control character in string literal at line 1 column 13 of the JSON data |
| Safari | JSON Parse error: Unterminated string |
Common causes
A line break inside a string
Text copied from a multi-line field, a log or a textarea keeps its real line breaks. They are not allowed inside a JSON string.
Input
{"a": "line1 line2"}JSON Formatter says
Line break inside a string. Use \n instead
A tab inside a string
A tab character pasted into a value fails the same way. Other control characters below the space character do too.
Input
{"a": "tab here"}JSON Formatter says
Control character inside a string. Escape it as \uXXXX
The escaped form works
Written as the two characters backslash and n, the line break is valid. JSON.stringify does this for you.
How to fix it
- Build the JSON with JSON.stringify(value) or json.dumps(value), not by joining strings.
- Replace each real line break inside a string with \n, and each tab with \t.
- If you assemble JSON from a template, escape the values before you insert them.
- Paste the text into the JSON formatter: it shows the line and column of the first bad character.