Nifty Dev Tools
  1. Home
  2. Guides
  3. Bad control character

Bad control character in string literal in JSON

A raw line break or tab inside a JSON string. JSON needs them escaped, as \n and \t. Safari calls it “Unterminated string”.

The exact message

A line break inside a string (the worked example below)
{
  "id": 7,
  "note": "Line one
Line two"
}
Chrome, Edge, Node.js
SyntaxError: Bad control character in string literal in JSON at position 32 (line 3 column 20)
Firefox
SyntaxError: JSON.parse: bad control character in string literal at line 3 column 20 of the JSON data
Safari
SyntaxError: JSON Parse error: Unterminated string
A tab inside a string
{"a": "x	y"}
Chrome, Edge, Node.js
SyntaxError: Bad control character in string literal in JSON at position 8 (line 1 column 9)
Firefox
SyntaxError: JSON.parse: bad control character in string literal at line 1 column 9 of the JSON data
Safari
SyntaxError: JSON Parse error: Unterminated string

Measured in Chrome 158, Firefox 159, Safari's engine (WebKit 27.2) and Node.js 22. Edge uses the same engine as Chrome.

Safari says “Unterminated string”, the same message it gives for a string that is never closed: to its parser the line break ends the string too early.

What it means

Inside a JSON string, the characters below U+0020 (line breaks, tabs, carriage returns and the other control characters) must be written as escapes: \n for a line break, \t for a tab, \r for a carriage return and \u0000 to \u001f for the rest. A raw one in the text is a “bad control character”.

What causes it

  • JSON built by gluing strings together, like '{"note": "' + note + '"}' or a template literal, where the value contains a line break or a tab. Nothing escapes it on the way.
  • Multi-line text typed or pasted into a JSON file: an address, a message, a PEM key or certificate.
  • A tab from a spreadsheet or an editor that inserts tabs instead of spaces, inside a string.
  • Shell scripts that put a variable into JSON with echo or printf, when the variable holds more than one line.

How to find the spot

Chrome, Node.js and Firefox point at the control character itself. In the example below, line 3 column 20 is the end of the line "note": "Line one: the line break. With Windows line endings, it's the \r of the \r\n. Tabs are harder to see, because they look like spaces.

The JSON Formatter points at the opening quote of that string instead, line 3, column 11, and says the string isn't closed before the line ends: you see which string to fix, even when it's a long one.

How to fix it

Let a JSON library write the string; it escapes every control character. In JavaScript:

const note = "Line one\nLine two";
const bad = `{"note": "${note}"}`; // JSON.parse(bad) throws
const good = JSON.stringify({ note });
// {"note":"Line one\nLine two"}

In shell scripts, let jq build the JSON: jq -n --arg note "$NOTE" '{note: $note}'. In a JSON file you edit by hand, replace the line break with \n (and a tab with \t), or press Fix it in the JSON Formatter: it escapes tabs and line breaks inside strings when the string is closed properly later.

Worked example

A note with a line break typed inside the string:

The input

{
  "id": 7,
  "note": "Line one
Line two"
}
Chrome and Node.js say
SyntaxError: Bad control character in string literal in JSON at position 32 (line 3 column 20)
The JSON Formatter says
Hmm, something's off on line 3, column 11: string is not closed before the line ends 🤔

Press “Fix it” and the JSON Formatter makes these changes:

  • Escaped a tab or line break inside a string (line 3)

The result:

{
  "id": 7,
  "note": "Line one\nLine two"
}

Open this example in the JSON Formatter

The value is the same as before (two lines of text), it's just written the way JSON allows.