10 Validating JSON

JSON is strict. A single misplaced comma or missing quote makes the entire file invalid. Validation checks that your JSON follows the rules.

Common Validation Errors

ErrorWhat it meansFix
Unexpected tokenA character was found where it wasn't expectedCheck for stray commas, extra brackets, or unquoted strings
Expected ',' or '}'A comma or closing brace is missingAdd the missing comma or brace
Expected '"'A string is missing its opening or closing quoteAdd double quotes around the key or string value
Unexpected numberA number appeared where it isn't validCheck for leading zeros or misplaced periods
Trailing commaA comma after the last element in an object/arrayRemove the comma after the last item

How to Validate JSON

Option 1 — Online validators: Paste your JSON into websites like jsonlint.com or jsonformatter.org. They highlight errors instantly.

Option 2 — Code editors: VS Code, Sublime Text, and others highlight JSON errors automatically. Look for red squiggly lines.

Option 3 — Command line: Use tools like jq or language-specific parsers:

# Using jq (Linux/macOS)
jq . file.json

# Using Python
python -m json.tool file.json

# Using Node.js
node -e "JSON.parse(require('fs').readFileSync('file.json','utf8'))"

Example: Fixing Invalid JSON

Invalid:

{
    "name": "Alice",
    age: 30,            // key not in double quotes
    "hobbies": ["art"]  // valid
}

Valid:

{
    "name": "Alice",
    "age": 30,          // added double quotes
    "hobbies": ["art"]
}

Pretty-Printing

Minified JSON is hard to read. Use pretty-printing to format it with indentation:

// Python
print(json.dumps(data, indent=2))

// JavaScript
JSON.stringify(data, null, 2)