JSON Schema Validation: Common Pitfalls
Fix JSON validation errors: trailing commas, type mismatches, encoding issues, nested object validation, and schema draft compatibility. Practical debugging guide.
The Problem
JSON parse errors give unhelpful messages like 'Unexpected token at position 42' with no indication of what went wrong. Schema validation errors are even worse: they tell you a value is invalid but not why your schema is not matching.
JSON is the most common data format on the web, but its strict syntax means a single misplaced comma crashes your entire application. JSON Schema validation adds another layer of complexity with type coercion, draft compatibility, and validation scope. This guide covers the 5 most common JSON validation pitfalls and how to fix each one.
Common errors covered
Trailing commas break JSON parsing
SyntaxError: Unexpected token } in JSON at position 42
SyntaxError: Expected property name or '}' in JSON
JSON does not allow trailing commas after the last element in objects or arrays. JavaScript objects do, which makes copy-pasting from JS code a common source of this error.
Step-by-step fix
- 1 Paste the JSON into the JSON Formatter - it highlights the exact error position.
-
2
Find the last property before each
}or]. - 3 Remove the comma after the last element.
- 4 Validate again to check for nested trailing commas.
{
"name": "Alice",
"roles": ["admin", "user",],
"age": 30,
}
{
"name": "Alice",
"roles": ["admin", "user"],
"age": 30
}
Single quotes or unquoted keys rejected
SyntaxError: Unexpected token ' in JSON at position 1
SyntaxError: Expected property name enclosed in double quotes
JSON requires double quotes for all strings and keys. Single quotes, backtick strings, and unquoted keys are JavaScript syntax but invalid JSON.
Step-by-step fix
- 1 Replace all single quotes with double quotes.
- 2 Wrap all object keys in double quotes.
- 3 Use the JSON Formatter to validate after changes.
-
4
In code, always use
JSON.stringify()to generate JSON - never construct it manually.
{'name': 'Alice', age: 30, status: `active`}
{"name": "Alice", "age": 30, "status": "active"}
Large integers lose precision silently
(No parse error - number silently rounds)
JSON.parse('{"id": 9007199254740993}').id === 9007199254740992
JavaScript uses 64-bit floating point (IEEE 754) for all numbers. Integers above 2^53 - 1 (9007199254740991) lose precision when parsed. This is a major issue for database IDs, Snowflake IDs, and financial data.
Step-by-step fix
-
1
Check if your IDs exceed
Number.MAX_SAFE_INTEGER(9007199254740991). -
2
Store large integers as strings in JSON:
"id": "9007199254740993". -
3
Use
BigIntin JavaScript for large integer arithmetic. - 4 Use the JSON Formatter to inspect whether numbers are represented correctly.
{"snowflake_id": 9007199254740993}
// After JSON.parse: id becomes 9007199254740992 - off by 1!
{"snowflake_id": "9007199254740993"}
// String preserves exact value, parse with BigInt when needed
BOM or encoding issues cause invisible parse errors
SyntaxError: Unexpected token in JSON at position 0
(Note the invisible character before 'in')
A UTF-8 BOM (Byte Order Mark, \xEF\xBB\xBF) at the start of a JSON file is invisible in text editors but makes the first { unparseable. Files saved from Windows editors often include it.
Step-by-step fix
- 1 Use the Diff Checker to compare your file with a clean version.
-
2
Check for BOM:
hexdump -C file.json | head -1(look foref bb bf). - 3 Save as UTF-8 without BOM in your editor.
-
4
Strip programmatically:
json_str = json_str.lstrip('\ufeff').
// File starts with invisible BOM:
\xEF\xBB\xBF{"name": "Alice"}
// JSON.parse sees: BOM + { - fails on position 0
// Clean UTF-8 without BOM:
{"name": "Alice"}
// Or strip in code:
const clean = text.replace(/^\uFEFF/, '');
JSON.parse(clean);
Extra properties silently pass validation
(No validation error - extra fields accepted)
Payload with injected fields passes schema check
By default, JSON Schema allows additional properties not defined in the schema. This means typos in property names pass validation and unexpected fields slip through.
Step-by-step fix
-
1
Add
"additionalProperties": falseto your schema objects. - 2 Validate your payload against the schema and check for warnings.
- 3 Use the JSON Formatter to inspect the actual payload structure.
- 4 Use Diff Checker to compare expected vs actual fields.
// Schema without additionalProperties:
{"type": "object", "properties": {"name": {"type": "string"}}}
// Payload: {"name": "Alice", "admin": true} - passes! admin sneaks through
// Schema with strict validation:
{
"type": "object",
"properties": {"name": {"type": "string"}},
"additionalProperties": false,
"required": ["name"]
}
// Payload with admin: true - REJECTED
Debugging Approach
- 1 Paste the JSON into the JSON Formatter - it pinpoints the exact error position.
- 2 Check for invisible characters: BOM, zero-width spaces, non-breaking spaces.
- 3 Verify the error position matches what you expect - off-by-one means hidden character.
- 4 For schema validation, test each field individually to isolate which one fails.
- 5 Compare your JSON structure against the schema using Diff Checker.
Prevention Checklist
-
Always use
JSON.stringify()to generate JSON - never build it with string concatenation. - Validate JSON syntax before parsing with the JSON Formatter tool.
- Store large integers (> 2^53) as strings in JSON.
- Save files as UTF-8 without BOM.
-
Add
"additionalProperties": falseto all JSON Schema objects. - Use a linter (ESLint, Prettier) to catch syntax issues before runtime.
Frequently Asked Questions
Can JSON have comments?
No. Standard JSON (RFC 8259) does not support comments. Use JSONC (JSON with Comments) or JSON5 if you need comments, but strip them before parsing with standard JSON.parse(). Many tools and configs (like tsconfig.json) actually use JSONC, not strict JSON.
Why does JSON require double quotes but JavaScript doesn't?
JSON is a data serialization format designed for interoperability between languages. Double-quote-only keeps the spec simple and unambiguous. JavaScript objects are code constructs with more flexible syntax. Use JSON.stringify() to convert JS objects to valid JSON.
What is the maximum size of a JSON document?
The JSON spec has no size limit. Practical limits depend on the parser: JavaScript's JSON.parse() can handle hundreds of megabytes but may cause out-of-memory errors. For large datasets, consider streaming JSON parsers or paginated APIs.
Related Debug Guides
Related Tools
Still stuck? Try our free tools
All tools run in your browser, no signup required.