Data & JSON5 min read

How to Validate JSON (And Fix Common Errors)

A single misplaced comma or missing bracket can break your entire application. Here's how to validate JSON and understand the most common errors.

JSON is one of the most widely used data formats in software development. APIs, configuration files, databases, and app settings all rely on it. But JSON has strict syntax rules, and a single misplaced comma or missing bracket can cause your entire application to fail silently.

Here's how to validate JSON, understand the most common errors, and fix them quickly.

Why JSON Validation Matters

Unlike plain text, JSON has zero tolerance for syntax errors. If even one character is out of place, the entire document fails to parse. Common consequences include:

  • API calls that return empty data instead of a useful error message
  • Configuration files that get silently ignored by the application reading them
  • Apps that crash on startup when they try to load a JSON config
  • Data pipelines that fail silently and produce incomplete output

Because JSON errors don't always surface as obvious error messages (especially in production systems), catching them early during development saves significant debugging time.

Common JSON Errors

Understanding the most frequent mistakes makes them easier to spot and fix.

1. Trailing Commas

This is the single most common JSON error. JSON does not allow a comma after the last item in an object or array.

// ❌ Invalid
{
  "name": "Alice",
  "age": 30,
}

// ✅ Valid
{
  "name": "Alice",
  "age": 30
}

JavaScript allows trailing commas in objects and arrays, so developers often carry this habit into JSON unintentionally.

2. Unquoted Keys

In JSON, all object keys must be wrapped in double quotes. JavaScript objects allow unquoted keys, but JSON does not.

// ❌ Invalid
{ name: "Alice" }

// ✅ Valid
{ "name": "Alice" }

3. Single Quotes Instead of Double Quotes

JSON requires double quotes everywhere. Single quotes are not valid.

// ❌ Invalid
{ 'name': 'Alice' }

// ✅ Valid
{ "name": "Alice" }

4. Unescaped Special Characters

If a string value contains a double quote or backslash, it must be escaped with a backslash.

// ❌ Invalid
{ "message": "She said "hello"" }

// ✅ Valid
{ "message": "She said \"hello\"" }

5. Mismatched Brackets or Braces

Every { must have a closing }, and every [ must have a closing ]. Missing or extra brackets are easy to introduce in long, deeply nested documents.

6. JavaScript-Specific Values

JavaScript has undefined, NaN, and Infinity, none of which are valid JSON values. JSON only supports null, true, false, numbers, strings, arrays, and objects.

// ❌ Invalid JSON
{ "value": undefined }
{ "result": NaN }

// ✅ Valid
{ "value": null }

How to Validate JSON

Method 1: Use an Online Validator

The quickest approach is to paste your JSON into the JSON Validator. A good JSON validator will:

  • Tell you immediately whether your JSON is valid
  • Highlight the exact line and column where an error occurs
  • Show you the structure (root type, depth, key count) for valid documents

This is the best option for spot-checking JSON from an API response, a config file, or documentation.

Method 2: Browser Developer Tools

Every browser's developer console can validate JSON instantly:

  1. Open DevTools (F12 or Cmd+Option+I)
  2. Go to the Console tab
  3. Type: JSON.parse('your json here')
  4. Press Enter

If the JSON is valid, it returns the parsed object. If not, it throws an error message with the exact position of the problem.

Method 3: Command Line with jq

jq is a powerful command-line JSON processor. To validate a file:

jq . input.json

If the file is valid, it pretty-prints the output. If not, it shows an error message with the line number.

Method 4: Your Code Editor

VS Code and most modern editors have JSON validation built in. Files saved with a .json extension are automatically checked, and errors appear as red underlines. You can also add a JSON Schema to a file to get property-level validation beyond just syntax.

Step-by-Step: Validating JSON Online

  1. Copy your JSON. This might be from an API response (use your browser's Network tab), a config file, or a data export.

  2. Paste into the validator. Open the JSON Validator tool and paste your content into the input field.

  3. Check the result. If your JSON is valid, you'll see the root type, nesting depth, and key count. If it's invalid, the error message shows the exact line and column of the problem.

  4. Fix the error. Go back to your source, find the indicated line, and correct the syntax.

  5. Re-validate. Paste the corrected JSON again to confirm it passes.

Reading JSON Error Messages

JSON error messages often look cryptic but follow predictable patterns:

  • "Unexpected token ','": You have a trailing comma somewhere
  • "Unexpected token 'u'": You have undefined in a value position
  • "Expected property name or '}'": A key is unquoted or missing
  • "Unexpected end of JSON input": A bracket or brace is unclosed

When you see an error, go to the line number indicated and look for one of the common mistakes above. In most cases, it's one of the first four errors listed.

After Validation: Formatting and Minifying

Once your JSON is valid, you may want to:

  • Format it for readability: add indentation so the structure is easy to read and review
  • Minify it for production: remove all whitespace to reduce file size

Both operations only make sense on valid JSON, which is why validation should always come first. A formatter that receives invalid JSON will report the error rather than attempt to format it.

Related Tools