JSON is strict. A single misplaced comma, an unquoted key, or a stray comment will cause a parse failure in any compliant JSON parser. Knowing how to validate quickly — and more importantly, how to read the error message and fix the problem — is a skill worth having locked in.

This guide covers every common validation method, from pasting into an online tool to validating programmatically in production code.

The Fastest Method — Online Validator

For one-off debugging, pasting into an online validator is the fastest path. A good validator does three things: tells you immediately whether the JSON is valid or not, gives you a precise error message with a line number and column position, and lets you fix and re-validate in the same window without copying back and forth.

Validate JSON instantly Paste your JSON to get a clear error message with line number. Also formats and minifies valid JSON.
Open JSON Validator →

Once the JSON is confirmed valid, use the JSON Formatter to prettify it with consistent indentation for easier reading.

Command Line Validation

For files on disk or output from a script, command line tools are faster than copy-pasting. Python's built-in json.tool module is the most universally available option — it ships with Python 3 and produces clear error messages.

# Validate a file python3 -m json.tool data.json # Validate piped input cat data.json | python3 -m json.tool # Validate and pretty-print in one step python3 -m json.tool data.json > pretty.json # jq — faster for large files, more features jq . data.json jq empty data.json && echo "valid" || echo "invalid"

jq is the more powerful option if you have it installed. jq empty validates without producing output — useful in scripts where you only need the exit code. Both return a non-zero exit code on invalid JSON, making them easy to integrate into shell scripts and CI pipelines.

Validating in Code

JavaScript / Node.js

The simplest approach is wrapping JSON.parse() in a try/catch. The error message includes a position indicator, though its format varies by engine.

// Basic validation function isValidJSON(str) { try { JSON.parse(str); return true; } catch (e) { return false; } } // With error detail function validateJSON(str) { try { return { valid: true, data: JSON.parse(str) }; } catch (e) { return { valid: false, error: e.message }; } }

Python

import json def validate_json(text): try: data = json.loads(text) return True, data except json.JSONDecodeError as e: return False, f"Line {e.lineno}, col {e.colno}: {e.msg}" # Validate a file with open('data.json') as f: try: data = json.load(f) print("Valid") except json.JSONDecodeError as e: print(f"Invalid: {e}")

Go

import ( "encoding/json" ) func isValidJSON(s string) bool { var js json.RawMessage return json.Unmarshal([]byte(s), &js) == nil }

CI/CD Pipeline

Validating JSON config files in CI prevents broken deployments. Add a validation step before deploy:

# GitHub Actions — validate all JSON files - name: Validate JSON files run: | find . -name "*.json" -not -path "*/node_modules/*" | while read f; do python3 -m json.tool "$f" > /dev/null || exit 1 done

The Most Common JSON Errors

ErrorInvalidValid
Trailing comma {"a": 1, "b": 2,} {"a": 1, "b": 2}
Single quotes {'key': 'value'} {"key": "value"}
Unquoted key {key: "value"} {"key": "value"}
Comment {"a": 1 // comment} {"a": 1}
Undefined value {"a": undefined} {"a": null}
NaN / Infinity {"a": NaN} {"a": null}
Unescaped newline in string {"a": "line1
line2"}
{"a": "line1\nline2"}
Missing comma between items {"a": 1 "b": 2} {"a": 1, "b": 2}

Reading JSON Error Messages

Error messages from different parsers look different but mean the same things. Here's how to read them.

Chrome/V8: SyntaxError: Unexpected token } in JSON at position 18 — the position number refers to the character offset from the start of the string. Count characters to find it, or paste into a validator that converts position to line and column.

Python: json.decoder.JSONDecodeError: Expecting ',' delimiter: line 3 column 5 (char 28) — Python gives you line and column directly. Line 3, column 5 is where the parser got confused, which is usually one character after the actual error.

jq: parse error (Invalid numeric literal at line 2, column 8) — similarly direct. The column points to where jq gave up, not always where the mistake is.

In all cases, the error position points to where the parser failed, which is often one or two characters after the actual mistake. If the validator says "unexpected token at line 3 column 8," look at line 3 columns 5–8 — the error is usually just before where the parser gave up.

JSON Schema Validation

Syntax validation only confirms the JSON can be parsed. Schema validation goes further — it checks whether the parsed data has the right structure, required fields, and value types. This is what you need for API request validation, config file enforcement, and data pipeline integrity checks.

The most widely used tool is Ajv for JavaScript and Node.js, and jsonschema for Python. Both implement the JSON Schema specification and return detailed validation errors.

// Ajv — JSON Schema validation in Node.js const Ajv = require("ajv"); const ajv = new Ajv(); const schema = { type: "object", properties: { name: { type: "string" }, age: { type: "integer", minimum: 0 } }, required: ["name", "age"] }; const validate = ajv.compile(schema); const valid = validate({ name: "Alice", age: 30 }); if (!valid) console.log(validate.errors);

Frequently Asked Questions

Why does my JSON look valid but still fail to parse?
The most common hidden culprits are: a trailing comma after the last item in an object or array (valid JavaScript, invalid JSON), single quotes instead of double quotes, a byte order mark (BOM) at the start of the file from certain text editors, or invisible Unicode characters like non-breaking spaces that look like regular spaces but break the parser. Use a validator that shows the exact line and column number to find it quickly — our JSON Validator pinpoints the position automatically.
What is the difference between JSON validation and JSON Schema validation?
JSON validation checks syntax only — can this string be parsed as JSON? JSON Schema validation checks structure — does this parsed JSON have the right shape, the right field types, and the required keys? Syntax validation is a prerequisite for schema validation. You need both in a production API: syntax validation at the parser level, schema validation in your application logic.
How do I validate JSON in a shell script?
The most portable one-liner is python3 -m json.tool file.json > /dev/null. It returns exit code 0 for valid JSON and non-zero for invalid, making it safe to use with && and || in shell scripts. If you have jq installed, jq empty file.json is faster for large files and produces no output on success.
Can JSON contain comments?
No. Standard JSON (RFC 8259) does not support comments. This is intentional — JSON is a data interchange format, not a configuration language. If you need comments in a config file, use JSONC (used by VS Code), JSON5, or YAML. Any JSON with // or /* */ comments will fail to parse in any RFC-compliant parser.
How do I validate a large JSON file efficiently?
For files under ~100MB, python3 -m json.tool or jq empty is fast enough. For very large files (hundreds of MB to GB), jq is significantly more memory-efficient because it uses a streaming parser. Avoid loading multi-gigabyte JSON files entirely into memory — use a streaming JSON parser like ijson in Python or stream-json in Node.js.

Related reading

Comparison
YAML vs JSON
Guide
How to Read a JWT Token