Back to the blog

Developer

Find the one character that breaks it.

Find out why JSON will not parse in your browser, nothing uploaded: trailing commas, single quotes, comments, unquoted names. Go to the line and fix it.

By 5 min read

A visual example of compact JSON becoming an indented document

JSON either parses or it does not, and a parser stops at the first character it cannot accept. Most invalid JSON is almost JSON: copied from JavaScript, a config file or a Python print. Here are the usual culprits, what the parser says about each, and how to fix them.

JSON is stricter than JavaScript.

RFC 8259 allows objects, arrays, numbers, true, false, null and strings in double quotes. Names must be strings in double quotes too. There are no comments, no trailing commas, no single quotes, and no NaN, Infinity or undefined.

Read where it stopped.

Paste the text into the JSON formatter. The line above the editors says Valid JSON, or Not valid JSON with the parser's own words and the line and column where it stopped; Go to error puts the cursor there.

The place it names is where the parser gave up, which is often just after the real mistake. A missing comma is reported at the start of the next value; an unclosed bracket where the wrong one closes it.

The usual culprits, and what the parser says.

A trailing comma after the last item stops with property name expected in an object and value expected in an array: delete the comma. Single quotes, names without quotes, and Python's True and None stop with invalid symbol at the first such character: use double quotes, quote every name, and write true, false and null.

A comment stops with invalid comment token: JSON has none, so take it out. Two values with no comma between them stop with comma expected. Two documents one after another, as in a log, stop with end of file expected: put them in an array, or read them as JSON Lines. A Windows path with single backslashes stops with invalid escape character: write each backslash twice.

Some causes cannot be seen. A curly quote pasted from a word processor, or a byte order mark at the very start of a file, stops with invalid symbol at that character.

Text, and what the formatter says
{"a": 1,}              property name expected
[1, 2,]                value expected
{'a': 1}               invalid symbol
{a: 1}                 invalid symbol
{"a": True}            invalid symbol
{"a": 1 // note
}                      invalid comment token
{"a":1}{"b":2}         end of file expected
{"p": "C:\Users"}      invalid escape character

Let a repair suggest, then read it.

Preview repair, under More transformations, proposes valid JSON for most of the cases above: quotes, names, comments, trailing commas and Python's constants, among others the jsonrepair library handles. It is a guess, and it can add or remove things. Read the preview before choosing Use result as source; your original stays as it was until you do.

Valid, and still wrong.

JSON can parse and still cause trouble. A name written twice is valid, but RFC 8259 says names should be unique, and programs differ on which value they keep; the formatter keeps both, points them out, and asks you to make them unique before querying, comparing or converting.

Large whole numbers are another quiet problem: JavaScript rounds integers beyond 9007199254740991, so the formatter keeps every number exactly as written. And a document can be valid JSON without being what a program expects; a JSON Schema checks that.

Further reading

Topics: why is my json invalid · invalid json error · json validator · json trailing comma · json single quotes · json comments · unexpected token in json · fix json online · json syntax error

A little less theory. A little more doing.

Try it on your own terms, with a free browser tool.

Try the tool