
JSON Format Validation and How to Fix the Most Common Parse Errors
Struggling with SyntaxError: Unexpected token? JSON parse errors usually follow patterns — trailing commas, single quotes, undefined values. Learn to diagnose and fix each type, plus how to automate validation.
What is JSON: Data Exchange Format Basics
JSON (JavaScript Object Notation) is the most widely used data exchange format in web development. It appears everywhere—API responses, configuration files, database exports, and more.
Basic JSON Structure
JSON consists of two data structures:
- Object:
{key: value}format - Array:
[value1, value2, ...]format
Basic JSON example:
{ "name": "Taro Tanaka", "age": 30, "isActive": true, "hobbies": ["reading", "travel", "programming"], "address": { "city": "Tokyo", "postalCode": "100-0001" }}
Data Types Available in JSON
- String: Enclosed in double quotes
"text" - Number:
123,45.67,-10, etc. - Boolean:
trueorfalse - null:
null - Object:
{} - Array:
[]
Important: JSON does not support undefined, functions, or Date objects.
6 Common Parse Errors and Their Causes
Here are typical error patterns encountered when parsing JSON.
Error 1: Trailing Comma
Error Message
SyntaxError: Unexpected token } in JSON at position 45
Problematic Code
{ "name": "Taro Tanaka", "age": 30, // ← This trailing comma is NG}
Solution
Remove the comma after the last property.
{ "name": "Taro Tanaka", "age": 30}
While JavaScript allows trailing commas in object literals, JSON strictly prohibits them.
Error 2: Single Quotes Used
Error Message
SyntaxError: Unexpected token ' in JSON at position 2
Problematic Code
{ 'name': 'Taro Tanaka' // ← Single quotes are NG}
Solution
Change all strings to double quotes.
{ "name": "Taro Tanaka"}
In JSON, both keys and values must always use double quotes.
Error 3: undefined / NaN / Infinity Mixed In
Error Message
SyntaxError: Unexpected token u in JSON at position 10
Problematic Code
{ "name": "Taro Tanaka", "email": undefined // ← undefined cannot be used in JSON}
Solution
Replace undefined with null, or remove the property entirely.
{ "name": "Taro Tanaka", "email": null}
Or
{ "name": "Taro Tanaka"}
Similarly, NaN and Infinity are also not allowed in JSON.
Error 4: Comments Written
Error Message
SyntaxError: Unexpected token / in JSON at position 15
Problematic Code
{ // This is a comment ← Comments not allowed in JSON "name": "Taro Tanaka"}
Solution
Remove all comments.
{ "name": "Taro Tanaka"}
JSON has no comment syntax. If comments are needed, create a dedicated key.
{ "_comment": "This is user information", "name": "Taro Tanaka"}
Error 5: Missing Key Quotes
Error Message
SyntaxError: Unexpected token n in JSON at position 3
Problematic Code
{ name: "Taro Tanaka" // ← Key has no quotes}
Solution
Always enclose keys in double quotes.
{ "name": "Taro Tanaka"}
While JavaScript objects allow key quotes to be omitted, JSON requires them.
Error 6: Numeric Key Mixed In
Error Message
SyntaxError: Unexpected number in JSON at position 5
Problematic Code
{ 123: "value" // ← Key is a number}
Solution
Convert numeric keys to strings.
{ "123": "value"}
JSON keys must always be strings.
Validation Methods: Browser, VS Code, Online Tools
There are several ways to verify if JSON is in the correct format.
Method 1: Validate in Browser Console
Open browser developer tools (F12), go to Console tab, and run:
JSON.parse('{"name": "Taro Tanaka", "age": 30}');
If no error appears, the JSON is in correct format. If an error occurs, it shows where the problem is.
Method 2: Use VS Code Extensions
In Visual Studio Code, opening a .json file automatically enables syntax highlighting and validation.
When errors exist:
- Red wavy underlines appear on relevant lines
- Error details display in Problems panel (Ctrl+Shift+M / Cmd+Shift+M)
Additionally, Alt+Shift+F (Windows) or Option+Shift+F (Mac) formats indentation and line breaks automatically.
Method 3: Online JSON Validator
Browser-based JSON formatter tools allow validation + formatting without installation.
Main features:
- Real-time syntax checking
- Error location highlighting
- Auto-formatting (indentation alignment)
- Compression (minify)
- Tree display
Simply paste your JSON, and error types and locations are instantly identified.
Method 4: Automated Validation with Node.js Script
For automated JSON file validation in CI/CD pipelines, use Node.js scripts.
const fs = require('fs');try { const data = fs.readFileSync('config.json', 'utf8'); JSON.parse(data); console.log('✅ JSON is correctly formatted');} catch (error) { console.error('❌ JSON parse error:', error.message); process.exit(1);}
Integrating this script into the build process enables automatic pre-deployment checks.
Debugging Techniques for Deeply Nested Cases
Finding errors in complex nested JSON is difficult. These techniques improve efficiency.
Technique 1: Parse Partially
To identify error locations, divide and parse JSON sections.
// Test parts instead of the wholeJSON.parse('{"name": "Taro Tanaka"}'); // OKJSON.parse('{"hobbies": ["reading", "travel",]}'); // Error detected
Technique 2: Format with JSON Formatter
Minified JSON without line breaks or indentation is hard to read, so format it first.
Before formatting:
{"name":"Taro Tanaka","age":30,"address":{"city":"Tokyo","postalCode":"100-0001"}}
After formatting:
{ "name": "Taro Tanaka", "age": 30, "address": { "city": "Tokyo", "postalCode": "100-0001" }}
Visibility improves, making error locations easier to spot.
Technique 3: Compare with Diff Tools
Comparing correct and incorrect JSON with diff tools (like VS Code's compare feature) makes differences obvious.
In VS Code:
- Select two files
- Right-click → "Compare Selected"
Differences are then highlighted.
Differences Between JSON and JavaScript Objects
JSON and JavaScript objects are similar but strictly different.
| Aspect | JSON | JavaScript Object |
|---|---|---|
| Key quotes | Required (double only) | Optional |
| Value quotes | Double only | Single also OK |
| Trailing commas | Not allowed | Allowed (ES5+) |
| Comments | Not allowed | Allowed |
| undefined | Not allowed | Allowed |
| Functions | Not allowed | Allowed |
| Date objects | Not allowed (convert to string) | Allowed |
Conversion Considerations
When converting JavaScript objects to JSON, JSON.stringify() automatically adjusts, but some data is lost.
const obj = { name: "Taro Tanaka", age: 30, greet: function() { return "Hello"; }, // Functions are removed undefined: undefined, // undefined is removed date: new Date() // Converted to ISO 8601 string};console.log(JSON.stringify(obj));// Output: {"name":"Taro Tanaka","age":30,"date":"2026-05-26T00:00:00.000Z"}
Automating JSON Validation in API Development
When developing web APIs, automating JSON validation for requests and responses prevents errors in advance.
Validation with JSON Schema
JSON Schema is a standard for defining JSON data structure.
Schema example:
{ "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "number", "minimum": 0 } }, "required": ["name", "age"]}
Validating against this schema ensures data integrity.
Implementation Example in Node.js (using ajv library)
const Ajv = require('ajv');const ajv = new Ajv();const schema = { type: "object", properties: { name: { type: "string" }, age: { type: "number", minimum: 0 } }, required: ["name", "age"]};const validate = ajv.compile(schema);const data = { name: "Taro Tanaka", age: 30 };if (validate(data)) { console.log('✅ Validation succeeded');} else { console.log('❌ Validation failed:', validate.errors);}
Integrating as middleware in web frameworks like Express automatically rejects invalid requests.
Summary: Knowing Error Patterns Makes It Easy
JSON parse errors are simple to solve once you know the patterns.
- Remove trailing commas
- Unify to double quotes (no single quotes)
- Convert undefined to null
- Remove comments
- Key quotes mandatory
Daily-use tools:
- Browser console (quick validation)
- VS Code (real-time checks during development)
- JSON formatter tool (instant online verification)
- JSON schema (automated API validation)
Looking at position X in error messages shows where the problem occurred. Stay calm and check one by one.
Use JSON formatter tools to debug efficiently.


