JSON Validation: Common Errors and How to Fix Them
What Is JSON Validation?
JSON (JavaScript Object Notation) is a text-based data format used everywhere: API responses, configuration files, database exports, log entries, and inter-service communication. Despite looking simple, JSON has strict syntax rules. A single misplaced character makes the entire document unparseable.
JSON validation checks whether a document conforms to the JSON specification (ECMA-404). A valid JSON document looks like this:
{
"name": "OmToolkit",
"version": "1.0.0",
"features": ["validation", "formatting"],
"config": {
"theme": "dark",
"notifications": true
}
}
Every JSON parser—whether in a browser, a backend server, or a CLI tool—applies the same rules. If any rule is violated, the parser throws an error and refuses to process the document.
JSON Syntax Rules You Must Remember
JSON is simpler than most programming languages, but its rules are absolute. There is no "mostly valid" JSON.
- Strings use double quotes. Single quotes and backticks are not valid.
- Property names must be quoted.
"name"is valid.namewithout quotes is not. - Commas separate items. Every property or array element except the last must be followed by a comma.
- No trailing commas. A comma after the last item in an object or array is invalid.
- Braces and brackets must match. Every
{needs a}, every[needs a]. - Values must be valid JSON types: string, number, boolean (
true/false),null, object, or array. - No comments. JSON does not support
//,/* */, or#comments. - Booleans and null are lowercase.
true,false,null—notTrue,FALSE, orNone.
Error: Single Quotes Instead of Double Quotes
This is the most common JSON mistake, especially for developers coming from Python or JavaScript.
Invalid:
{
'name': 'John',
'age': 30
}
The parser sees ' and immediately fails. JSON only accepts " for strings and property names.
Fixed:
{
"name": "John",
"age": 30
}
If you have a large document with single quotes, a find-and-replace from ' to " usually works, but be careful with strings that contain apostrophes.
Error: Trailing Comma
JavaScript allows trailing commas. JSON does not.
Invalid:
{
"name": "John",
"age": 30,
}
The comma after 30 is the problem. The parser expects another property after the comma but finds } instead.
Fixed:
{
"name": "John",
"age": 30
}
The same applies to arrays:
// Invalid
["a", "b", "c",]
// Valid
["a", "b", "c"]
Error: Missing Comma
Forgetting a comma between properties is easy to miss visually.
Invalid:
{
"host": "localhost"
"port": 5432
}
The parser reads "localhost", then expects a comma or }, but finds "port". Most parsers report the error at the line of the second property, not the first—because the parser doesn't know the comma is missing until it encounters the unexpected token.
Fixed:
{
"host": "localhost",
"port": 5432
}
Tip: When a parser reports an "unexpected token" error, check the line before the reported location for a missing comma.
Error: Missing or Extra Brackets
Bracket mismatches often happen in deeply nested structures.
Missing closing bracket:
{
"users": [
{ "name": "Alice" },
{ "name": "Bob" }
}
The ] to close the array is missing. The parser reaches the final } and reports an unexpected end of input.
Fixed:
{
"users": [
{ "name": "Alice" },
{ "name": "Bob" }
]
}
Formatting nested JSON makes bracket issues much easier to spot. A JSON formatter indents the structure so you can visually trace the nesting.
Error: Unquoted Property Names
JavaScript allows unquoted object keys. JSON does not.
Invalid:
{
name: "John",
age: 30
}
Fixed:
{
"name": "John",
"age": 30
}
This commonly happens when copying JavaScript object literals into a JSON file.
Error: Comments Inside JSON
JSON does not support comments of any kind.
Invalid:
{
// database configuration
"host": "localhost",
"port": 5432
}
Also invalid:
{
"host": "localhost", /* primary */
"port": 5432
}
If you need commented configuration files, consider YAML (which supports comments) or JSONC (JSON with Comments), a non-standard extension supported by VS Code and some tools. Standard JSON parsers will reject both.
Fixed:
{
"host": "localhost",
"port": 5432
}
Error: Invalid Boolean or Null Values
JSON is case-sensitive for its keyword values.
Invalid:
{
"enabled": True,
"data": None
}
True and None are Python conventions. JSON requires lowercase:
Fixed:
{
"enabled": true,
"data": null
}
Other invalid values: FALSE, NULL, undefined, NaN, Infinity. None of these are valid JSON.
JSON vs JavaScript Object Syntax
JSON was inspired by JavaScript objects but is a separate, stricter format. Here are the key differences:
| Feature | JavaScript Object | JSON |
|---|---|---|
| Property names | Quoted or unquoted | Must be double-quoted |
| Strings | Single or double quotes | Double quotes only |
| Trailing commas | Allowed | Not allowed |
| Comments | Allowed | Not allowed |
undefined |
Valid value | Not valid |
| Functions | Can be values | Not valid |
When you see a "JSON parse error" after copying a JavaScript object, the problem is almost always one of these differences.
Syntax Validation vs JSON Schema Validation
There are two distinct kinds of JSON validation, and it's important not to confuse them.
Syntax validation
Answers: "Is this valid JSON?"
This checks the document against the JSON grammar. Does it parse? Are quotes, commas, and brackets correct? A JSON validator checks this.
Schema validation
Answers: "Does this valid JSON have the structure my application expects?"
For example, this is syntactically valid JSON:
{
"username": 12345,
"email": true
}
But if your application requires username to be a string and email to be a string containing @, the document fails schema validation even though it passes syntax validation.
JSON Schema (a separate specification) lets you define these structural rules formally. Most everyday JSON validation errors are syntax problems, not schema problems.
How to Troubleshoot a JSON Error
When a parser reports an error, follow this process:
- Read the error message. Most parsers report a line number and column, or a character position. Go to that location.
- Check the character before the reported position. Missing commas and unclosed quotes often cause the parser to fail on the next token.
- Check commas. Look for missing commas between properties or trailing commas after the last item.
- Check quotes. Are all strings and keys using double quotes? Are there any unescaped quotes inside strings?
- Check brackets. Count
{vs}and[vs]. They must match. - Check values. Are booleans lowercase? Is
nullspelled correctly? Are there any bare words? - Format the JSON. Paste it into a formatter to reveal the structure. Many bracket and nesting issues become obvious when the JSON is properly indented.
- Validate the complete document. Fix one error at a time and re-validate. Fixing the first error often resolves cascading errors downstream.
Using JSON Tools
Different tools help at different stages of working with JSON:
- JSON Validator — Paste JSON and immediately see whether it's valid. If not, the validator identifies the error location and type. Use this when you have a document that's failing to parse and you need to find the problem.
- JSON Formatter — Takes minified or poorly formatted JSON and adds consistent indentation. Essential for inspecting deeply nested structures where bracket-matching issues hide.
- JSON Minifier — Removes all whitespace from valid JSON. Useful when preparing JSON for transmission or storage where size matters. Only works on valid JSON—fix errors first, then minify.
- JSON to YAML / YAML to JSON — Convert between formats. If you need comments in a configuration file, consider converting to YAML, adding comments there, and converting back to JSON for consumption.
Validation Checklist
Quick reference for reviewing JSON before submitting or committing:
- All strings use double quotes (
") - All property names are quoted
- No trailing commas after the last item
- Commas between every property and array element
- All
{have matching}, all[have matching] - Boolean values are lowercase:
true,false - Null is lowercase:
null - No comments (
//,/* */,#) - No
undefined,NaN, orInfinity - Strings containing quotes have them escaped:
\"
Conclusion
JSON validation errors are almost always caused by a small set of syntax mistakes: wrong quotes, missing commas, trailing commas, unmatched brackets, or invalid values. The strict syntax exists because JSON is a data interchange format—parsers across every language and platform need to agree on exactly what's valid.
When you hit a parse error, read the error message, check the line before the reported location, and work through the checklist above. Most errors resolve in under a minute once you know what to look for.