TeachyLeaf library

What are you looking for?

Search practical tutorials and browser-based tools.

JSON · Debugging

Common JSON syntax errors

Learn to spot common JSON mistakes and fix invalid data with clear before-and-after examples.

JSON looks simple, but it is strict. One missing character can turn a valid payload into an invalid one. In practice, many JSON errors come from a short list of mistakes: missing commas, trailing commas, unquoted keys, mismatched brackets, and unescaped quotes.

Who this is for: beginners who need to read or fix a JSON object or array.

What you will learn: recognize common JSON syntax mistakes, use a parser's reported position as a clue, and correct invalid text with before-and-after examples.

Prerequisites: no programming framework is required; you only need to recognize braces, brackets, commas, and quoted text.

Practice and limits: Try each correction in the browser-based JSON formatter, which reports syntax errors locally. A successful parse confirms syntax only.

A JSON parser checks whether the text follows JSON syntax. It does not check whether the data has the fields, types, or values a particular API or application requires. For example, {"name":"Asha"} can be valid JSON and still be rejected by an API that requires an email field.

When a parser says invalid JSON, use its reported position as a clue, not a guaranteed location of the mistake. Some parsers point to where they could no longer continue, which may be just after the missing comma or quote. Read the surrounding lines and inspect the structure from the nearest opening brace or bracket.

1. Missing comma between fields

Object properties must be separated by commas.

{
  "name": "Asha"
  "email": "asha@example.com"
}

This fails because the comma between "name" and "email" is missing.

{
  "name": "Asha",
  "email": "asha@example.com"
}

2. Trailing commas

JSON does not allow a trailing comma before the closing brace or bracket.

{
  "id": 101,
  "name": "Asha",
}

The correct version removes the final comma:

{
  "id": 101,
  "name": "Asha"
}

The same rule applies to arrays:

{
  "tags": ["php", "sql", "json",]
}

Remove the final comma to fix it:

{
  "tags": ["php", "sql", "json"]
}

3. Unquoted keys

JSON object keys must always be wrapped in double quotes.

{
  name: "Asha",
  email: "asha@example.com"
}

Correct format:

{
  "name": "Asha",
  "email": "asha@example.com"
}

4. Unquoted string values

String values also need double quotes. Numbers and booleans can be unquoted, but text cannot.

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

Fix it by quoting the string:

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

5. Quotes inside strings

If a text value contains a quote character, escape it with a backslash.

{
  "message": "He said, "hello" to everyone."
}

Correct version:

{
  "message": "He said, \"hello\" to everyone."
}

6. Missing closing braces or brackets

JSON must be balanced. Every { needs a closing }, and every [ needs a closing ].

{
  "customer": {
    "id": 101,
    "name": "Asha"
}

Close the object before the final brace:

{
  "customer": {
    "id": 101,
    "name": "Asha"
  }
}

7. Using single quotes

JSON does not allow single quotes around strings or keys.

{
  'name': 'Asha'
}

Use double quotes instead:

{
  "name": "Asha"
}

8. Invalid numeric format

Numbers must be valid JSON numbers. A decimal should not have a second dot, and scientific notation must be complete.

{
  "price": 12.34.56,
  "rate": 1e
}

Correct version:

{
  "price": 12.34,
  "rate": 1e3
}

JSON numbers cannot have leading zeroes (except for the value 0 itself), a leading plus sign, or values such as NaN and Infinity. If one of these appears, use the JSON representation your application expects, such as a quoted string when the value is meant to be text.

9. Comments inside JSON

JSON does not allow comments. If you copy data from a JavaScript file, remove comment syntax before trying to parse it.

{
  // this is a comment
  "name": "Asha"
}

Use this instead:

{
  "name": "Asha"
}

How to debug a parser error

  1. Copy the exact input and note the parser's line, column, or character position if it provides one.
  2. Inspect the reported spot and the text immediately before it. Check for a missing comma, quote, colon, or closing bracket.
  3. Check each object property in order: quoted key, colon, value, then a comma only when another property follows.
  4. After fixing one error, parse the input again. A parser may report only the first problem it encounters.
  5. If parsing succeeds but the application still rejects the data, compare its shape and value types with that application's requirements.

Quick debugging checklist

  • Check that every opening brace or bracket has a matching closing character.
  • Make sure keys and string values use double quotes.
  • Remove trailing commas.
  • Verify commas separate entries, but do not place one after the last item.
  • Escape inner quotes inside strings.
  • Use parser positions as clues; the actual mistake may be immediately before the reported position.
  • Remember that valid syntax does not guarantee that data meets an API's schema or business rules.

Before and after example

Here is a small payload with several issues:

{
  "customer": {
    "id": 101
    "name": "Asha",
    "active": true,
    "tags": ["new", "student",],
  }
}

Fixed version:

{
  "customer": {
    "id": 101,
    "name": "Asha",
    "active": true,
    "tags": ["new", "student"]
  }
}

JSON is strict, but the rules are simple. Once you check commas, quotes, and nesting, many syntax errors become easier to spot. After it parses, check the data against the requirements of the API, form, configuration file, or other system that will use it.