How to Validate JSON: Complete Guide for Developers
JSON syntax errors are one of the most common headaches in web development. A single missing comma or misplaced quote can break your entire API response. Here's everything you need to know about validating JSON correctly.
What is valid JSON?
JSON (JavaScript Object Notation) has strict syntax rules defined by RFC 8259. Unlike JavaScript objects, JSON has zero tolerance for deviation. Here are the rules:
- Strings must use double quotes — single quotes are not valid JSON
- Keys must be strings — wrapped in double quotes
- No trailing commas — the last item in an object or array cannot have a comma after it
- No comments — JSON does not support // comments or /* block comments */
- Values can only be: strings, numbers, booleans (true/false), null, objects, or arrays
- No undefined — undefined is not a valid JSON value
- Numbers cannot have leading zeros — 007 is invalid, 7 is valid
The 7 most common JSON errors
1. Single quotes instead of double quotes
This is the number one mistake. JavaScript accepts single quotes, but JSON does not.
2. Trailing commas
A comma after the last property or array element is invalid JSON, even though JavaScript allows it.
3. Unquoted keys
Every key in a JSON object must be a double-quoted string. Unquoted keys work in JavaScript but fail in JSON.
4. Comments in JSON
JSON does not support comments of any kind. If you need comments, consider JSONC (JSON with Comments) or YAML instead.
5. Missing commas between properties
Every property except the last needs a comma after it.
6. Unescaped special characters in strings
Characters like newlines, tabs, and backslashes must be escaped: \n, \t, \\.
7. Using undefined or NaN
Only null is valid for empty values. undefined, NaN, and Infinity are not valid JSON.
Validate your JSON instantly
Paste your JSON and get instant validation with syntax highlighting and error detection.
Open JSON Formatter →How to validate JSON (4 methods)
Method 1: Online validator (fastest)
The quickest way is to paste your JSON into an online JSON validator. You get instant feedback with the exact line and column of any error. No setup required.
Method 2: Command line with jq
If you're on Linux or Mac, jq is the standard tool:
Method 3: Python
Method 4: Node.js
Using an online JSON validator
Our JSON Formatter & Validator does more than just validate — it formats, beautifies, minifies, and syntax-highlights your JSON. It runs 100% in your browser, so your data never leaves your machine.
Key features that save time:
- Auto-detection — paste JSON and it validates automatically
- Error pinpointing — shows exact line and column of syntax errors
- Syntax highlighting — color-coded keys, strings, numbers, and booleans
- Minification — compress JSON with one click and see byte savings
- Depth analysis — see nesting depth and structure at a glance
Validating JSON in production code
In production, you should validate JSON at every boundary — API inputs, configuration files, message queues, and webhook payloads. Here are the patterns:
API input validation
Never trust incoming JSON. Always wrap parsing in try/catch and validate the schema, not just the syntax. JSON can be syntactically valid but semantically wrong (missing required fields, wrong data types).
Configuration file validation
Validate config files at startup, not at runtime. If config.json is malformed, fail fast with a clear error message rather than crashing later with a cryptic error.
Schema validation
For strict validation, use JSON Schema — it lets you define expected structure, required fields, data types, and value constraints. Libraries like ajv (JavaScript) and jsonschema (Python) make this straightforward.
Best practices for working with JSON
- Always use a formatter — reading minified JSON is needlessly painful
- Validate at boundaries — check JSON at every system boundary (API, file, queue)
- Use consistent indentation — 2 spaces is the most common standard
- Lint your config files — add JSON linting to your CI/CD pipeline
- Keep it flat — deeply nested JSON (5+ levels) is hard to read and debug
- Use descriptive keys — userName is better than u
Try the JSON Formatter now
Free, fast, and 100% client-side. Format, validate, and beautify JSON instantly.
Open JSON Formatter →