tigtools

How to Fix Common JSON Syntax Errors

Learn how to fix trailing commas, single quotes, unquoted keys, comments, NaN, undefined, and BOM issues—and how to interpret JSON line and column errors.

By TIG Tools editorial teamPublished October 25, 20265 min read

JSON errors usually come from small differences between JavaScript object notation and valid JSON. JSON requires double-quoted strings and property names, does not allow comments or trailing commas, and has a limited set of value types. This guide shows how to spot common mistakes, correct them, and use line and column details to find the problem.

Start with a strict JSON check

JSON syntax is defined by RFC 8259. A JSON value can be an object, array, string, number, true, false, or null. Object property names must be strings in double quotes. Whitespace is allowed around JSON values and punctuation, but comments are not part of the format.

Paste your text into the JSON Validator to check whether it parses. After fixing the syntax, use the JSON Formatter to make the structure easier to inspect. A formatter can only format valid input; it cannot reliably guess what you intended to write.

Trailing commas

A comma separates items or properties. It must be followed by another item or property, so a comma is not allowed immediately before a closing ] or }.

Invalid:

{
  "name": "Mina",
  "active": true,
}

Valid:

{
  "name": "Mina",
  "active": true
}

The same rule applies to arrays. Remove the comma after the final value. Be careful when editing by hand: a comma is needed between neighboring properties, but not after the last one.

Single quotes

JSON strings and property names use double quotes. Single quotes may be accepted in some programming languages, but they are not valid JSON delimiters.

Invalid:

{'name': 'Mina'}

Valid:

{"name": "Mina"}

If a string itself contains a double quote, escape it with a backslash: "She said \"hello\".". A backslash can also escape another backslash, as in "C:\\files\\report.json".

Unquoted object keys

Every object key must be a JSON string, even if it looks like an ordinary identifier. Quote keys such as id, total, and isActive with double quotes.

Invalid:

{orderId: 4812, isActive: true}

Valid:

{"orderId": 4812, "isActive": true}

The rule applies to nested objects too. If a parser points to a key or to the colon after it, check that the key is surrounded by double quotes and that a colon separates it from its value.

Comments

Standard JSON does not support line comments (//) or block comments (/* ... */). Remove comments before parsing, or store the explanation as a normal string property if it belongs in the data.

Invalid:

{
  // Customer's order
  "orderId": 4812
}

Valid JSON without the comment:

{
  "orderId": 4812
}

A comment may be meaningful in a configuration format that extends JSON, but that does not make it valid standard JSON. If the destination expects JSON, use a strict validator to check the final text.

NaN, undefined, and other non-JSON values

JavaScript has values that JSON does not. NaN and undefined are not valid JSON values. Neither are Infinity or -Infinity. Use a valid number, null, or omit the property, depending on what the data should mean.

Invalid:

{"score": NaN, "nickname": undefined}

Valid, if the score is unavailable and the key should remain:

{"score": null, "nickname": null}

Use null only when it accurately represents the data. If a value is unknown, unavailable, or not applicable, choose a consistent representation for the system that will read the JSON. When JSON is produced from JavaScript, check the serialized output rather than assuming every JavaScript value can be represented directly.

A BOM at the beginning of a file

A UTF-8 byte order mark (BOM) is the byte sequence EF BB BF. It may appear at the start of a text file and can be invisible in an editor. RFC 8259 says JSON exchanged between systems must not begin with a BOM, though parsers may choose to ignore one. As a result, one parser may accept a file that another rejects.

If an error appears before the first visible character, check whether the file begins with a BOM. Save or export it as UTF-8 without a BOM, or remove the leading mark with an editor that can display encoding details. Do not remove a U+FEFF character from inside a quoted string unless it is unwanted data; the issue described here is the mark before the JSON text.

How to read line and column errors

A parser often reports the first place where it can no longer continue, not necessarily the original mistake. For example, a trailing comma may be reported at the closing brace because the parser expected another property after the comma. A missing comma may only become obvious when the parser reaches the next key.

Use this process:

  1. Go to the reported line and inspect the character at or just before the reported column.
  2. Check nearby punctuation: quotes, commas, colons, and closing brackets or braces.
  3. Look at the preceding line too. The actual issue may be an unclosed string or missing separator before the reported location.
  4. Fix the first error and validate again. Later errors can be consequences of the first one.

Line and column numbering varies by parser. Some count from 1 and others from 0; some count characters differently when non-ASCII text or tabs appear. Treat the location as a useful starting point, not a universal coordinate. If the error message gives an excerpt or caret, use that alongside the line and column.

Worked example: repair an order object

This order-like example combines several common mistakes:

{
  // delivery order
  orderId: 4812,
  'customer': 'Mina',
  items: [
    {"sku": "A17", "qty": 2,},
  ],
  total: NaN,
  note: undefined
}

A strict parser may stop at the comment first. After removing it, quote the keys and string values with double quotes. Then remove the trailing commas and replace the non-JSON values with values that match the data. For example:

{
  "orderId": 4812,
  "customer": "Mina",
  "items": [
    {"sku": "A17", "qty": 2}
  ],
  "total": 24.95,
  "note": null
}

Here, 24.95 is a valid JSON number, and null is used to retain the note field without a value. If the note is not needed, remove that property instead. Validate the corrected text before sending it to another application or saving it as a JSON file.

Summary

  • Remove commas after the final item or property; JSON does not allow trailing commas.
  • Use double quotes for every string and object key, and remove comments.
  • Replace NaN and undefined with an appropriate valid value or omit the property.
  • If an error occurs at the start of a file, check for a UTF-8 BOM.
  • Treat line and column numbers as clues, check nearby syntax, and validate again after each repair.

Try it now

Use the JSON Validator for free, right in your browser. No sign-up, and your files stay on your device.

Open JSON Validator

More guides