How to Validate JSON — Online Validator, Common Errors & Fixes

JSON is strict. A single trailing comma, a JavaScript-style comment, or a single-quoted string will break a parser entirely. This guide covers every common JSON syntax error, how to validate JSON programmatically in JavaScript, Python, and the command line, and how to go further with JSON Schema validation.

What Is JSON Validation?

JSON validation means checking that a string conforms to the JSON specification (RFC 8259). A valid JSON document can be fully parsed by any JSON parser without errors.

There are two levels of validation:

  • Syntax validation — Is the JSON parseable? Does it follow the grammar rules?
  • Schema validation — Does the parsed JSON have the expected structure, types, and values?

Common JSON Syntax Errors

These are the errors that trip up developers most often, along with fixes:

1. Trailing comma

The most common error. JSON does not allow a comma after the last element.

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

2. Single quotes instead of double quotes

JSON requires double quotes for both keys and string values. Single quotes are a JavaScript-ism, not valid JSON.

// Invalid {'name': 'Alice'} // Valid {"name": "Alice"}

3. Comments

JSON does not support comments. Use JSONC (JSON with Comments) for config files that need them — but convert to plain JSON before parsing.

// Invalid — comments not allowed in JSON { // user record "name": "Alice" /* primary user */ }

4. Unquoted keys

Unlike JavaScript objects, JSON keys must always be quoted strings.

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

5. Invalid primitives (undefined, NaN, Infinity)

JSON only supports: string, number, object, array, true, false, null. JavaScript values like undefined, NaN, and Infinity are not valid JSON.

// Invalid {"value": undefined, "ratio": NaN, "limit": Infinity} // Valid — use null for missing values {"value": null, "ratio": null, "limit": 1e308}

Validate JSON in JavaScript

The simplest approach is JSON.parse() inside a try/catch. The error message typically includes the character position of the problem.

function validateJSON(str) { try { JSON.parse(str); return { valid: true, error: null }; } catch (err) { return { valid: false, error: err.message }; } } // Usage const result = validateJSON('{"name": "Alice",}'); // { valid: false, error: "JSON Parse error: Unexpected token }" }

For Node.js scripts that need to validate a JSON file on disk:

import { readFileSync } from 'fs'; function validateJSONFile(path) { const raw = readFileSync(path, 'utf8'); try { const parsed = JSON.parse(raw); console.log('Valid JSON. Keys:', Object.keys(parsed)); } catch (err) { console.error('Invalid JSON:', err.message); } }

Validate JSON in Python

Python's built-in json module raises a json.JSONDecodeError for invalid input. The error includes line, column, and a message.

import json def validate_json(text: str) -> dict: try: parsed = json.loads(text) return {"valid": True, "data": parsed} except json.JSONDecodeError as e: return { "valid": False, "error": str(e), "line": e.lineno, "col": e.colno, } # Validate a JSON file with open("data.json") as f: result = validate_json(f.read()) print(result)

Validate JSON from the CLI

jq is the standard command-line JSON processor. It exits with a non-zero code on invalid JSON:

# Validate and pretty-print jq . data.json # Validate only (exit code 0 = valid, 5 = invalid) jq empty data.json && echo "Valid" || echo "Invalid" # With Python (no extra tools needed) python3 -m json.tool data.json # With Node.js one-liner node -e "JSON.parse(require('fs').readFileSync('data.json','utf8'))" && echo "Valid"

JSON Schema Validation

Syntax validation only confirms the JSON is parseable. JSON Schema validation goes further — it checks that the parsed data matches your expected structure: required fields, data types, string formats, numeric ranges, and more.

Here is a simple schema for a user object, validated with ajv:

import Ajv from 'ajv'; const schema = { type: 'object', properties: { id: { type: 'integer' }, name: { type: 'string', minLength: 1 }, email: { type: 'string', format: 'email' }, role: { type: 'string', enum: ['admin', 'user', 'viewer'] }, }, required: ['id', 'name', 'email'], additionalProperties: false, }; const ajv = new Ajv({ allErrors: true }); const validate = ajv.compile(schema); const data = { id: 1, name: 'Alice', email: 'alice@example.com', role: 'admin' }; if (validate(data)) { console.log('Valid!'); } else { console.log('Errors:', validate.errors); }

Use DevKit's JSON Schema Validator to test a schema and JSON document interactively in your browser.

Related Tools

Related Guides

FAQ

What makes JSON invalid?

The most common causes of invalid JSON are: trailing commas after the last item in an array or object, using single quotes instead of double quotes for strings, unquoted keys, comments (JSON does not support comments), and undefined or NaN values which are not valid JSON primitives.

How do I validate JSON in JavaScript?

Use JSON.parse() inside a try/catch block. If it throws a SyntaxError, the JSON is invalid. The error message usually includes the line and character position of the problem. For structured validation against a schema, use the ajv library.

What is JSON Schema validation?

JSON Schema is a vocabulary that lets you describe the structure of your JSON data — required fields, data types, allowed values, string patterns, and more. Tools like ajv (JavaScript) and jsonschema (Python) validate a JSON document against a schema and report which fields fail.

Can I validate JSON online for free?

Yes. DevKit's JSON Formatter & Validator parses your JSON in the browser, highlights syntax errors, and formats the output. Your data never leaves your device.

What is the difference between JSON validation and JSON formatting?

Validation checks whether the JSON is syntactically correct (parseable). Formatting (pretty-printing) arranges valid JSON with consistent indentation and line breaks for readability. You must have valid JSON before you can format it.