What makes JSON valid?

A JSON document is valid when it follows the rules of the JSON specification (RFC 8259). There are two levels of correctness:

  • Syntax validity — the text is well-formed JSON: correct brackets, quoted keys, no trailing commas, no comments.
  • Schema validity — the data has the expected structure: required fields exist, values have the right type, numbers are within allowed ranges.

A JSON parser validates syntax. JSON Schema validates structure. Both matter — but you need syntax to be valid first.

This site's JSON Formatter validates syntax live and points to the exact line where parsing fails.

Common JSON errors

These are the mistakes that most often break JSON documents:

✕ Common mistake Trailing comma
Invalid
{ "name": "Jane", "age": 30, }
Valid
{ "name": "Jane", "age": 30 }

The comma after the last property is illegal in JSON. JavaScript allows it — JSON does not.

✕ Common mistake Single quotes instead of double quotes
Invalid
{'name': 'Jane'}
Valid
{"name": "Jane"}

JSON requires double quotes for both keys and string values. Single quotes are a JavaScript convenience, not valid JSON.

✕ Common mistake Unquoted keys
Invalid
{name: "Jane", age: 30}
Valid
{"name": "Jane", "age": 30}

All object keys must be quoted strings. This is the most common mistake for developers coming from JavaScript object literals.

✕ Common mistake Comments
Invalid
{ "timeout": 30 // seconds }
Valid
{ "timeout": 30 }

JSON has no comment syntax. Remove comments before parsing, or switch to JSONC / YAML for config files that need documentation.

✕ Common mistake undefined, NaN, Infinity
Invalid
{"value": undefined, "ratio": NaN}
Valid
{"value": null, "ratio": 0}

JSON only allows: string, number, boolean, null, object, array. undefined, NaN, and Infinity are JavaScript values with no JSON equivalent.

JSON syntax rules — quick reference

RuleValidInvalid
String quotes"hello"'hello'
Object keys"key": 1key: 1
Trailing comma[1, 2, 3][1, 2, 3,]
Null value"x": null"x": undefined
Booleantrue / falseTrue / False
Number3.14.14 or 01
Root value{} or []bare string

Validate JSON in code

Every major language has a built-in JSON parser. Wrap the parse call in a try/catch to validate:

JavaScript / Node.js

javascripttry {
  const data = JSON.parse(str)
  console.log('Valid JSON', data)
} catch (e) {
  console.error('Invalid JSON:', e.message)
}

Python

pythonimport json

try:
    data = json.loads(s)
    print('Valid JSON', data)
except json.JSONDecodeError as e:
    print('Invalid JSON:', e)

PHP

php$data = json_decode($str, true);
if (json_last_error() !== JSON_ERROR_NONE) {
    echo 'Invalid JSON: ' . json_last_error_msg();
}

Go further with JSON Schema

Syntax validation only checks that the JSON is parseable. JSON Schema lets you validate the content — required fields, value types, formats, and ranges.

example JSON Schema{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "required": ["name", "age", "email"],
  "properties": {
    "name":  { "type": "string", "minLength": 1 },
    "age":   { "type": "integer", "minimum": 0, "maximum": 150 },
    "email": { "type": "string", "format": "email" }
  }
}

With this schema, {"name": "Jane", "age": -5, "email": "jane@example.com"} would fail validation because age is below the minimum of 0 — even though it's perfectly valid JSON syntax.

Use the JSON Schema Generator to create a schema from any valid JSON, then refine it to add your constraints.

Frequently asked questions

No. Comments are not part of the JSON specification (RFC 8259). A JSON parser will reject any file that contains // or /* */ comments.

If you need comments in a config file, consider JSONC (JSON with Comments, used by VS Code) or YAML, which supports # comments natively.