Validate JSON

How to validate JSON (and fix common errors)

If your formatter says “invalid JSON”, it usually means the input isn’t strict JSON. Here’s how to spot and fix the most common issues.

1) Make sure keys and strings use double quotes

In JSON, both keys and string values must use double quotes.

// ❌ Not JSON
{a: 'hello'}

// ✅ Valid JSON
{"a": "hello"}

2) Remove trailing commas

Trailing commas are common when copying from languages like JavaScript.

// ❌ Not JSON
{"a": 1,}

// ✅ Valid JSON
{"a": 1}

3) JSON doesn’t allow comments

// ❌ Not JSON
{
  // comment
  "a": 1
}

// ✅ Valid JSON
{
  "a": 1
}

4) Watch for special values: NaN, Infinity, undefined

JSON supports null, true, false, numbers, strings, arrays, and objects. Values like undefined are not valid JSON.

5) If you’re comparing JSON, format first to reduce noise

Different whitespace/indentation makes diffs noisy. Format both JSON inputs first, then compare.

Next: How to compare two JSON files.