JSON errors

JavaScript and Python JSON parsers

Bad control character in string literal in JSON

JSON strings can't contain raw control characters such as a real line break or tab. They must be written as escape sequences like \n and \t. The parser found one typed directly inside a string.

How each browser and language words it

Chrome, Edge and Node 20+
SyntaxError: Bad control character in string literal in JSON at position 60 (line 3 column 31)
Firefox
SyntaxError: JSON.parse: bad control character in string literal at line 3 column 31 of the JSON data
Python
json.decoder.JSONDecodeError: Invalid control character at: line 3 column 31 (char 60)

Paste your JSON to find the exact line

The body string has real line breaks and a tab inside it. Press Repair to escape them.

Skip to editor
Explorer
Open Files

No files open

0 files open

jsoneditor.io v2

{
  "title": "Release notes",
  "body": "Fixed the login bug
Added dark mode
	and a settings page"
}
JSONUTF-8
Spaces: 2

Causes and how to fix each one

1. A multi-line string pasted into a value

Pasting an address, a SQL query or a stack trace into a JSON string keeps its line breaks. Replace each line break with \n, or press Repair to escape them all.

2. Building JSON from text without escaping it

Inserting user input or file contents into a JSON template by hand skips the escaping. JSON.stringify and json.dumps escape control characters for you.

Python
import json

note = "line one\nline two"
payload = json.dumps({"note": note})  # writes "line one\\nline two"

3. Python strict mode

Python's json.loads(text, strict=False) accepts raw control characters inside strings. It's handy for reading messy data you don't control, but the JSON is still invalid for every other parser.

Questions people ask

Which characters have to be escaped in JSON strings?

Double quotes, backslashes and every control character from U+0000 to U+001F. The common ones have short forms like \n, \t and \r, and the rest are written as \u0000 style escapes.

How do I put a line break in a JSON string?

Write it as \n inside the string. When the JSON is parsed it becomes a real line break in the resulting value.

Tools for this