How to Validate JSON: Common Errors and Fixes
Your API returns a 400 error. The log says "Invalid JSON." You stare at the payload and everything looks fine β until you spot the single quote hiding on line 47. JSON validation errors are frustrating precisely because they are often tiny mistakes that break everything.
The Most Common JSON Syntax Errors
1. Trailing Commas
The number one JSON mistake. JavaScript accepts trailing commas, so developers carry the habit into JSON where it is invalid.
{
"name": "Alex",
"age": 30
}
Remove the comma after the last value. Always.
2. Single Quotes Instead of Double Quotes
JSON requires double quotes. This is valid JavaScript but invalid JSON: {'name': 'Alex'}. Replace all single quotes with double quotes.
3. Unquoted Keys
Every key in JSON must be a quoted string. {name: "Alex"} should be {"name": "Alex"}.
4. Missing Commas Between Elements
Easy to miss when editing JSON manually β forgetting the comma between key-value pairs.
5. Mismatched Brackets
Opening a { but closing with ], or nesting incorrectly. Use a JSON Validator that highlights matching brackets.
How to Validate JSON Effectively
Quick Manual Validation
Paste your JSON into our JSON Validator. It highlights the exact line and character position of any error. All processing happens in your browser β your data never leaves your machine.
Programmatic Validation
For applications, validate JSON at every boundary. In JavaScript, wrap JSON.parse() in a try-catch. In Python, use json.loads() with exception handling.
Schema Validation
Syntax validation tells you JSON is well-formed. Schema validation tells you it has the right structure. Use our JSON Schema Validator to define expected shapes.
Debugging JSON Errors Step by Step
- Format first β Paste into a JSON Formatter to get proper indentation
- Check the error position β Most parsers report line and column
- Look for the usual suspects β Trailing commas, single quotes, unquoted keys
- Validate incrementally β For large JSON, validate sections separately
- Compare versions β Use JSON Diff against a working version
Setting Up Automated Validation
In Your Editor
Enable JSON syntax highlighting, linters, and schema validation for config files.
In Your CI Pipeline
Add JSON validation to your build process to catch errors before they merge.
In Your API
Never trust incoming JSON. Validate both syntax and schema at your API boundary. Return helpful error messages.
Frequently Asked Questions
What is the difference between JSON validation and JSON Schema validation?
JSON validation checks syntax (proper quotes, brackets, commas). JSON Schema validation checks structure β right fields, right types, right constraints.
Why does my JSON work in JavaScript but fail in other parsers?
JavaScript is more lenient than the JSON spec. It allows trailing commas, single quotes, and comments. Strict parsers reject all of these.
Related Resources
- JSON Formatting Best Practices β write cleaner JSON from the start
- JSON Schema Validation Guide β enforce data contracts
- JSON Validator Tool β validate JSON syntax instantly
- JSON Formatter Tool β beautify and fix JSON formatting