JSON Validator with Error Messages: Find & Fix Errors Fast
Nothing is more frustrating than a JSON parse error with no indication of where the problem is. This guide covers every common JSON syntax error, how to read error messages, and how to fix them in seconds using a validator that gives you line numbers and column positions.
Why "Invalid JSON" Is Not Enough
The world's worst JSON error message is: invalid json. No line number, no column, no description of what went wrong. For a 10-line JSON object, you can scan manually. For a 500-line API response, you will spend 20 minutes hunting a single misplaced comma.
A good JSON validator does three things:
- Tells you what is wrong (trailing comma, single quotes, missing bracket, etc.)
- Tells you where (line number and column)
- Shows the error in context so you can see the surrounding structure
Our JSON Formatter and Validator uses the browser's native JSON.parse() engine to produce the same detailed error messages you would see in Chrome DevTools or Node.js, then maps the character position back to line and column numbers so you can jump straight to the problem.
The 10 Most Common JSON Syntax Errors
Error 1: Trailing Comma
The most common JSON mistake. JavaScript allows trailing commas; JSON does not.
// INVALID - trailing comma after last element
{
"name": "Alice",
"age": 30,
}
// VALID
{
"name": "Alice",
"age": 30
}
Error message: SyntaxError: Unexpected token } in JSON at position 32
Fix: Remove the comma after the last key-value pair.
Error 2: Single Quotes Instead of Double Quotes
// INVALID - single quotes
{'name': 'Alice'}
// VALID - double quotes only
{"name": "Alice"}
Error message: SyntaxError: Unexpected token ' in JSON at position 1
Fix: Replace all single quotes with double quotes. Use Find & Replace carefully - values containing apostrophes (it's) need to be escaped as it\'s or restructured.
Error 3: Unquoted Keys
// INVALID - JavaScript object literal, not JSON
{name: "Alice", age: 30}
// VALID
{"name": "Alice", "age": 30}
Error message: SyntaxError: Unexpected token n in JSON at position 1
Fix: Quote every key. This is the most common mistake when copying JavaScript object literals to use as JSON.
Error 4: Comments
// INVALID - JSON does not support comments
{
// This is the user object
"name": "Alice",
"age": 30 /* admin user */
}
Fix: Remove all comments. If you are working with a config file that needs comments, use JSONC (.jsonc) or YAML instead.
Error 5: Missing Comma Between Items
// INVALID - missing comma between elements
{
"name": "Alice"
"age": 30
}
// VALID
{
"name": "Alice",
"age": 30
}
Error message: SyntaxError: Unexpected string in JSON at position 20
Fix: Add a comma after every key-value pair except the last one.
Error 6: Unclosed Bracket or Brace
// INVALID - missing closing bracket
{
"users": [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
// VALID
{
"users": [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
]
}
Error message: SyntaxError: Unexpected end of JSON input
Fix: Paste into a formatter that highlights bracket matching. Every { needs a }, every [ needs a ].
Error 7: Undefined, NaN, or Infinity
// INVALID - these are JavaScript values, not JSON
{
"value": undefined,
"ratio": NaN,
"limit": Infinity
}
// VALID - use null or strings
{
"value": null,
"ratio": null,
"limit": "Infinity"
}
Fix: Replace undefined and NaN with null. If you need to preserve the distinction, use strings like "NaN" or add a type field.
Error 8: Unescaped Special Characters in Strings
// INVALID - unescaped backslash and newline
{"path": "C:\Users\Alice", "note": "line1
line2"}
// VALID
{"path": "C:\\Users\\Alice", "note": "line1\nline2"}
JSON string escape sequences: \" (quote), \\ (backslash), \/ (slash), \n (newline), \r (carriage return), \t (tab), \uXXXX (unicode).
Error 9: Duplicate Keys
// TECHNICALLY VALID per spec, but a logical error
{
"status": "active",
"status": "inactive"
}
// JSON.parse() accepts this - last value wins
// "status" will be "inactive"
Duplicate keys are not a syntax error - JSON.parse() will not throw. But they indicate a bug. The last value silently overwrites earlier ones. Always check your data generation logic.
Error 10: Wrong Value Types
// Common mistakes
{
"active": "true", // string, not boolean
"count": "42", // string, not number
"data": "null" // string, not null
}
// Correct
{
"active": true,
"count": 42,
"data": null
}
These are not syntax errors - they parse successfully. But they cause type-checking bugs downstream. A boolean string "true" is truthy in JavaScript, but so is any non-empty string, meaning conditional logic behaves correctly by accident. When the comparison is strict (=== true), it breaks.
Validate JSON Instantly with Error Messages
Paste your JSON to get the exact error type, line number, and column position highlighted. Free, 100% in-browser, nothing sent to any server.
Open JSON Validator →Step-by-Step: Debugging a JSON Error
- Read the error message. It contains the character position. For example:
Unexpected token , at position 142means there is a problem at the 142nd character of the string. - Convert position to line:column. Count newlines before position 142, or paste into a validator that does this for you.
- Look at the character and its neighbors. The reported position is often one character past the actual problem. If it reports a
}, the real error is usually one line above (a missing comma before the closing brace). - Format the JSON first. Paste minified JSON into a formatter before trying to debug it. Structure is invisible in a single-line blob.
- Validate the structure. Use bracket matching to verify every
{has a}and every[has a]. - Check for non-ASCII characters. Invisible characters like BOM (
\uFEFF), zero-width spaces, or non-breaking spaces can cause parse failures that are invisible in most text editors.
How to Write a JSON Validator in JavaScript
Here is a compact, production-ready validator that returns structured error info:
function validateJSON(jsonString) {
try {
const parsed = JSON.parse(jsonString);
return { valid: true, parsed, error: null };
} catch (e) {
// Extract position from error message
const posMatch = e.message.match(/position (\d+)/);
const pos = posMatch ? parseInt(posMatch[1]) : null;
let line = null;
let column = null;
if (pos !== null) {
const before = jsonString.slice(0, pos);
const lines = before.split('\n');
line = lines.length;
column = lines[lines.length - 1].length + 1;
}
return {
valid: false,
parsed: null,
error: {
message: e.message,
position: pos,
line,
column
}
};
}
}
// Usage
const result = validateJSON('{"name": "Alice",}');
if (!result.valid) {
console.error(`Error at line ${result.error.line}, col ${result.error.column}: ${result.error.message}`);
}
Validating Structure with JSON Schema
Syntax validation only checks that your JSON is parseable. It does not check that it has the right structure for your application. For that, use JSON Schema:
// Schema: user must have name (string) and age (integer, min 0)
const schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["name", "age"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 0 },
"email":{ "type": "string", "format": "email" }
},
"additionalProperties": false
}
Use our JSON Schema Validator to test a JSON document against a schema and get detailed per-field error messages.
Frequently Asked Questions
Why does my JSON validator say it is valid but my app still crashes?
Syntax validation only confirms the JSON is parseable. Your app may be expecting specific fields, types, or values that are missing or wrong. Use JSON Schema validation to enforce structure. Also check that you are passing the parsed object, not the JSON string, to your app's logic.
My JSON has thousands of lines. How do I find the error quickly?
A good validator with line numbers is the fastest approach. Paste into our tool - it shows the error line highlighted. Alternatively, use Node.js: node -e "JSON.parse(require('fs').readFileSync('file.json','utf8'))" which prints the position. Then use your editor's Go-to-Line feature (Ctrl+G or Cmd+G).
What is the difference between JSON validation and JSON Schema validation?
JSON validation checks syntax: is this a valid JSON string? JSON Schema validation checks semantics: does this JSON have the right structure, types, and values? You need both. Syntax first (parse it), then schema (validate the shape).
My JSON is valid but has wrong values. How do I catch that?
Write a JSON Schema with constraints: "minimum", "maximum", "enum", "pattern", "format". For example, "format": "email" validates email format, "enum": ["active","inactive"] restricts to those two values. Run the schema validator in your CI pipeline to catch data quality issues before they reach production.
Can I validate JSON without sending it to a server?
Yes. Our JSON Formatter runs entirely in your browser using the native JavaScript engine. No data is transmitted. This is important for JSON containing credentials, PII, or internal API responses.
Validate your JSON now with line-number errors → Open the free JSON Validator.
Usman has 10+ years of experience securing enterprise infrastructure, managing high-traffic servers, and building zero-knowledge security tools. Read more about the author.