"Bad control character in string literal": line breaks inside JSON strings
JSON strings cannot contain raw line breaks or tabs. See the errors in Chrome, Node.js and Python and how to escape them.
Error messages you may see
| Chrome · Edge · Node.js | SyntaxError: Bad control character in string literal in JSON at position 16 (line 1 column 17) |
|---|---|
| Python json.loads | json.decoder.JSONDecodeError: Invalid control character at: line 1 column 17 (char 16) |
| This tool | Line 1, Col 17: Line break inside a string (use \n) |
Why it happens
A JSON string cannot contain a real line break, tab or other control character. They must be written as escape sequences such as \n and \t. This often happens when text is inserted into JSON by string concatenation.
Invalid
{"memo": "line 1↵
line 2"}
How to fix
Replace the line break inside the string with \n (and tabs with \t). Better still, build JSON with JSON.stringify() or json.dumps(), which escape these characters for you.
Fixed
{"memo": "line 1\nline 2"}
Open this example in the JSON formatter →
Tip: the formatter fixes this in one click with the 🔧 Auto-fix button.