Data Cleaning & File Converters

Fix Invalid JSON: Unexpected Token and Missing Comma

How to fix an Unexpected token error in JSON: read the line and column, find missing commas, single quotes, trailing commas and comments, then format the valid result.

PerkTonic Team4 min read
Cover of the guide "Fix Invalid JSON: Unexpected Token and Missing Comma"

An "unexpected token" error means the JSON parser reached a character that cannot appear at that point. To fix it you need the exact position and the cause, and the JSON Formatter & Validator gives both: it reports the line and the column and explains in words what is wrong. This guide shows how to read the message and lists the usual causes.

Read the error position

The validator reports the position where the parser stopped, as a line and a column. That is the place of the first character that breaks the rules, not always the place where the mistake was made. A missing comma, for example, is reported at the start of the next item, because the parser only notices the problem when it meets that item.

So read the error in two steps. Look at the reported position, then look at the text just before it. The cause is usually in the previous line or at the end of the previous value. The message under the position explains the likely cause in plain language.

Limits to keep in mind: pasted text can be up to 5 MB, and files up to 50 MB.

Worked example: missing comma

Take this JSON:

{
  "id": 7,
  "name": "Ana"
  "active": true
}

The comma after "Ana" is missing. The validator reports the error at line 4, column 3. Line 4 is the line with "active", and column 3 is its opening quotation mark, the first character that cannot follow a finished value without a comma.

JSON Formatter and Validator reporting a missing comma at line 4 column 3
The missing comma is reported at line 4, column 3, where the next key begins.

The fix is in line 3, not in line 4: add a comma after "Ana". Paste the text again and the error disappears, or the validator moves on to the next problem if there is one. Fix one error at a time and validate again, because a parser stops at the first error and later ones are not shown.

The usual causes

The validator explains these cases with their own messages:

  • Single quotes. JSON strings use double quotes, so 'name' is invalid.
  • Unquoted keys. {name: "Ana"} is a JavaScript object, not JSON. Every key needs double quotes.
  • Trailing commas. A comma after the last item in an object or array is not allowed.
  • Comments. JSON has no comment syntax, so // and /* */ are errors.
  • NaN, True or other non-JSON values. JSON has true, false and null in lower case, and no NaN or Infinity. Python-style True and None are common in data copied from a script.
  • Leading zeros. A number such as 007 is invalid. Write 7, or use a string if the zeros matter.
  • Text after the end. Anything after the closing brace or bracket, such as a second object or a log line, is an error.
JSON Formatter and Validator explaining that single quotes are not valid in JSON
The message explains that JSON strings need double quotes.

For an input with single quotes, the explanation names the problem and points to the position, so you know that the quotes are the cause and not a bracket. The rules are set out in RFC 8259, and the JavaScript side is described on MDN under JSON.parse().

Duplicate keys are not always a syntax error, but they are a trap, because parsers keep one of them and drop the other. The validator reports duplicate keys with their position, so you can decide which value is the right one.

Comments and trailing commas (JSONC)

Configuration files such as editor settings often contain comments and trailing commas. That format is called JSONC. It is not valid JSON, so a strict parser rejects it with an unexpected token error at the first comment or the first trailing comma.

If the file is meant to be JSONC, load it with a parser that accepts it. In the validator, tick Allow comments and trailing commas (JSONC): the file is then accepted, a note counts the comments and the trailing commas, and the output is strict JSON without them. If the file must be strict JSON from the start, because an API or a library reads it, remove the comments and the trailing commas yourself and leave the option off.

A payload that comes from a webhook can also have extra text around the JSON, such as a header line. The Webhook Payload Formatter is made for these cases.

Format or minify once valid

When the validator reports no error, it says Valid JSON with the number of objects, arrays and values. Then format the JSON so that it is readable. The Output menu offers Beautify with 2 spaces, 4 spaces or tabs, and Minify for the smallest size, for example in a request body. A checkbox sorts the keys from A to Z.

Output menu of the JSON Formatter and Validator set to Beautify, 2 spaces
Once the JSON is valid, the Output menu formats it, for example with 2 spaces.

Formatting does not change the data. It only changes the whitespace, so the formatted and the minified versions parse to the same value.

Valid JSON can still be wrong for your purpose. If a field must have a certain type or a required key, use the optional section Validate against a JSON Schema in the same tool, which supports the drafts 2020-12, 2019-09, Draft-07 and Draft-04. Numbers such as 64-bit IDs lose digits in many parsers, so the validator keeps integers larger than 2^53 exactly and tells you how many it found. To pull a value out of a valid document, use the JSONPath & jq Tester.

Check your own file

Open the JSON Formatter & Validator and paste the JSON that fails. Read the line and the column, then read the text just before that position. Fix the cause, validate again, and format the result with 2 spaces.

Was this guide helpful?
Share

Comments