How to Validate JSON
A practical guide to checking JSON for syntax errors, understanding common mistakes, and formatting it for readability.
What makes JSON valid?
JSON (JavaScript Object Notation) has a strict syntax defined by RFC 8259. All keys must be double-quoted strings, values must be one of six types (string, number, object, array, boolean, null), and trailing commas are not allowed. These rules are stricter than JavaScript object literal syntax, which trips up many developers.
The most common JSON errors
Trailing commas after the last item are the single most common JSON mistake — they are valid in JS but illegal in JSON. Other frequent issues include single-quoted strings, unquoted keys, comments (not supported), and undefined or NaN values.
// Invalid JSON — trailing comma
{
"name": "Jane",
"role": "admin", // ← comma not allowed here
}
// Invalid JSON — single quotes
{
'name': 'Jane' // ← keys and strings must use double quotes
}
// Invalid JSON — unquoted key
{
name: "Jane" // ← keys must be quoted
}Valid JSON examples
Here is a reference of every valid JSON value type. Note that JSON objects and arrays can be nested arbitrarily deep.
// Object
{ "id": 1, "active": true, "score": 9.5, "tag": null }
// Array
[1, "two", false, null, { "nested": true }]
// String (top-level scalar is valid JSON)
"hello"
// Number
42Validating in JavaScript
Use JSON.parse() inside a try/catch to validate programmatically. The error message usually points to the line and column of the first syntax problem.
function isValidJson(str) {
try {
JSON.parse(str);
return true;
} catch {
return false;
}
}
isValidJson('{"key": "value"}'); // true
isValidJson("{key: 'value'}"); // falseValidating against a schema
Syntax validation only tells you the JSON is well-formed, not that it has the right shape for your application. JSON Schema lets you define the expected structure — required fields, types, value ranges — and validate documents against it. Libraries like ajv (Node.js) or jsonschema (Python) implement the JSON Schema spec.
// JSON Schema example
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["id", "name"],
"properties": {
"id": { "type": "integer" },
"name": { "type": "string", "minLength": 1 },
"role": { "type": "string", "enum": ["admin", "user", "guest"] }
}
}Are comments allowed in JSON?
No. Comments are not part of the JSON specification. If you need annotated config files, consider JSONC (JSON with Comments) or YAML instead.
Why does my API return invalid JSON sometimes?
A common cause is a server returning an HTML error page (like a 500 or 404) when your code expects JSON. Check the Content-Type response header and the HTTP status code before parsing.
Is undefined a valid JSON value?
No. JSON supports only string, number, object, array, true, false, and null. undefined, NaN, and Infinity are JavaScript-specific values and will be stripped or cause errors when serialized with JSON.stringify.
How to Read a Diff
A practical guide to reading unified diffs — understand the +/- notation, hunk headers, file headers, and how diffs appear in git, pull requests, and patch files.
Read guide →How to Read and Write CSV
A practical guide to the CSV format — structure rules, edge cases with quotes and commas, parsing in JavaScript and Python, and common pitfalls.
Read guide →How to Convert JSON to YAML (and Back)
A practical guide to converting between JSON and YAML — format differences, conversion rules, gotchas with types, and code examples.
Read guide →