DevToolsForYou
Data Formats

How to Validate JSON

A practical guide to checking JSON for syntax errors, understanding common mistakes, and formatting it for readability.

2 min readUpdated Apr 11, 2026

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.

json
// 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.

json
// 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
42

Validating 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.

javascript
function isValidJson(str) {
  try {
    JSON.parse(str);
    return true;
  } catch {
    return false;
  }
}

isValidJson('{"key": "value"}'); // true
isValidJson("{key: 'value'}");   // false

Validating 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
// 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"] }
  }
}
Frequently asked questions

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.

Related cheatsheetsAll cheatsheets →
Related guidesAll guides →