How to Validate JSON Online
Invalid JSON is one of the most common causes of parsing errors in APIs and config files. This guide covers how to spot and fix JSON syntax errors, and the difference between syntax validation and schema validation.
Two types of JSON validation
Syntax validation checks whether a string is valid JSON — whether it can be parsed without errors. This is what JSON.parse() does.
Schema validation checks whether the parsed JSON matches an expected structure — correct field names, required fields present, values of the right type. This requires a schema (written in JSON Schema) in addition to the data.
Most debugging starts with syntax validation. Schema validation is useful once you're building an API contract or validating config files programmatically.
Common JSON syntax errors
1. Trailing commas
The most common JSON error. JavaScript objects and arrays allow trailing commas; JSON does not.
// Invalid JSON
{
"name": "Alice",
"age": 30, ← trailing comma
}
// Valid JSON
{
"name": "Alice",
"age": 30
}2. Single quotes instead of double quotes
JSON requires double quotes for both keys and string values. Single quotes are not valid.
// Invalid JSON
{ 'name': 'Alice' }
// Valid JSON
{ "name": "Alice" }3. Unquoted keys
JavaScript objects allow unquoted keys; JSON does not.
// Invalid JSON (JavaScript object literal, not JSON)
{ name: "Alice", age: 30 }
// Valid JSON
{ "name": "Alice", "age": 30 }4. Comments
JSON does not support comments. If you're writing config files and need comments, consider YAML or JSONC (JSON with Comments, supported by VS Code and TypeScript).
// Invalid JSON
{
// This is a comment
"name": "Alice"
}
// Valid JSON — no comments
{ "name": "Alice" }5. Undefined and NaN
undefined, NaN, and Infinity are JavaScript values with no JSON equivalent. They cause errors during serialization.
// These are not valid JSON values
{ "value": undefined }
{ "ratio": NaN }
{ "limit": Infinity }
// Use null for missing values, or a sentinel number for Infinity
{ "value": null }
{ "ratio": null }
{ "limit": 1e308 }6. Unescaped special characters in strings
Certain characters inside JSON strings must be escaped: \n for newline,\t for tab, \" for a literal double quote, \\ for a literal backslash.
// Invalid — literal newline inside string
{ "message": "line one
line two" }
// Valid — escaped newline
{ "message": "line one
line two" }
// Invalid — unescaped quote
{ "title": "He said "hello"" }
// Valid
{ "title": "He said \"hello\"" }7. Numbers: leading zeros, hex, no quotes
// Invalid
{ "id": 007 } // leading zeros not allowed
{ "hex": 0xFF } // hex literals not allowed
{ "val": .5 } // must be 0.5
// Valid
{ "id": 7 }
{ "hex": 255 }
{ "val": 0.5 }How to validate JSON in different environments
Browser console
// Paste in DevTools console
JSON.parse('{"name": "Alice"}') // → parsed object, no error
JSON.parse('{name: "Alice"}') // → SyntaxError: Unexpected token nNode.js
const fs = require('fs')
const raw = fs.readFileSync('data.json', 'utf8')
try {
const data = JSON.parse(raw)
console.log('Valid JSON')
} catch (e) {
console.error('Invalid JSON:', e.message)
}Python
import json
try:
data = json.loads(raw_string)
print("Valid JSON")
except json.JSONDecodeError as e:
print(f"Invalid JSON: {e}")Command line
# Using jq (prints formatted JSON or error)
cat data.json | jq .
# Using Python one-liner
python3 -m json.tool data.jsonSchema validation: beyond syntax
Syntax validation only tells you the JSON is parseable. It doesn't tell you whether a required field is missing or a value has the wrong type. That's where JSON Schema comes in.
// Syntactically valid, but semantically wrong
{ "user": { "email": 12345, "age": "thirty" } }
// A schema catches these:
{
"type": "object",
"properties": {
"user": {
"type": "object",
"required": ["email"],
"properties": {
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0 }
}
}
}
}See the JSON Schema tutorial for a full introduction to writing schemas.
Validate your JSON now
Paste any JSON into Jsonic's JSON Formatter & Validator for instant syntax checking with clear error messages. For schema validation, use the JSON Schema Validator. Both run entirely in your browser.