Bad control character in string literal
One of the strings in your JSON contains an actual line break or tab. It’s easy to miss, because the text looks normal on screen. JSON doesn’t allow these characters inside a string as they are. They have to be written as escapes: \n for a line break and \t for a tab. Python reports the same problem as Invalid control character at.
Quick fix
- Go to the position in the message. You’ll usually find a quoted value that runs onto a second line.
- Replace the line break with
\n, or the tab with\t. - Or paste the JSON below to escape all of them at once.
Fix your JSON
How the line break got there
Almost always, the JSON was built by gluing strings together instead of using a JSON library. A multi-line address, a comment from a text box, or a paragraph pasted from a document brings its line breaks along, and they land in the JSON as they are. Editing a JSON file by hand and pressing Enter inside a value does the same thing.
Fixing it where the JSON is made
Let a JSON library write the text for you. JSON.stringify and json.dumps escape every control character automatically, so the problem can’t happen. If you have to read JSON that already contains them, Python’s strict=False option will accept it:
// JavaScript: stringify escapes line breaks and tabs
JSON.stringify({ note: "line one\nline two" }); // {"note":"line one\nline two"}
# Python: json.dumps does too, and strict=False reads text that has them
json.dumps({"note": "line one\nline two"})
json.loads(text, strict=False)To escape a single piece of text by hand, use the JSON escape tool.
The same error elsewhere
| Chrome, Edge, Node.js 20+ | Bad control character in string literal in JSON at position 44 |
|---|---|
| Python | json.decoder.JSONDecodeError: Invalid control character at: line 3 column 25 (char 44) |
| Firefox | JSON.parse: bad control character in string literal |
| Go | invalid character '\n' in string literal |
| PHP | Control character error, possibly incorrectly encoded |