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.
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.
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.
Python
Go
CI/CD Pipeline
Validating JSON config files in CI prevents broken deployments. Add a validation step before deploy:
The Most Common JSON Errors
| Error | Invalid | Valid |
|---|---|---|
| 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.
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 havejqinstalled,jq empty file.jsonis 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.toolorjq emptyis fast enough. For very large files (hundreds of MB to GB),jqis significantly more memory-efficient because it uses a streaming parser. Avoid loading multi-gigabyte JSON files entirely into memory — use a streaming JSON parser likeijsonin Python orstream-jsonin Node.js.