fjordtools / Errors / Bad control character in JSON

Bad control character in string literal in JSON at position 12

A string in the JSON contains a raw control character, most often a line break or a tab typed straight into the text. JSON does not allow that: inside a string a line break must be written as \n and a tab as \t. Build the JSON with JSON.stringify instead of joining strings by hand.

Fjordtools team · Seen in Chrome and Node (V8) · Published · 1 min read

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, olderUnexpected token in JSON at position 12
FirefoxJSON.parse: bad control character in string literal at line 1 column 13 of the JSON data
SafariJSON Parse error: Unterminated string

Common causes

  1. 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
  2. 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
  3. 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

  1. Build the JSON with JSON.stringify(value) or json.dumps(value), not by joining strings.
  2. Replace each real line break inside a string with \n, and each tab with \t.
  3. If you assemble JSON from a template, escape the values before you insert them.
  4. Paste the text into the JSON formatter: it shows the line and column of the first bad character.

Related errors

More error explainers are coming

See all errors.