Invalid JSON is one of the most common causes of API errors, broken configuration files, and deployment failures. This guide covers the most frequent JSON mistakes, introduces JSON Schema for structural validation, and compares modern alternatives.
Common JSON Syntax Errors
| Error | Invalid | Valid |
|---|---|---|
| Trailing comma | {"a": 1, "b": 2,} | {"a": 1, "b": 2} |
| Single quotes | {'key': 'value'} | {"key": "value"} |
| Unquoted keys | {key: "value"} | {"key": "value"} |
| Comments | {"a": 1 // comment} | {"a": 1} |
| Single value | undefined | null |
| Hex numbers | {"n": 0xFF} | {"n": 255} |
| Infinity/NaN | {"n": Infinity} | {"n": null} |
JSON Schema
JSON Schema is a vocabulary that allows you to validate the structure and content of JSON data — not just syntax. It defines types, required fields, value constraints, and relationships between fields.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["name", "email"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0 }
}
}
Schema Validation Tools Compared
| Tool | Language | TypeScript Types | Runtime Validation | Best For |
|---|---|---|---|---|
| JSON Schema | Any (via libraries) | Via generation tools | Yes (Ajv, etc.) | API contracts, cross-language |
| Zod | TypeScript/JS | Native (schema-first) | Yes | TypeScript projects |
| Joi | JavaScript | Via @types/joi | Yes | Node.js/Express APIs |
| Yup | JavaScript | Partial | Yes | Form validation (React) |
Linting vs Validation
- Linting: Checks syntax correctness — is this valid JSON? Can it be parsed? (JSON.parse test)
- Validation: Checks structural correctness — does this JSON conform to a schema? Are required fields present? Are values within constraints?
Both are important. A valid JSON object that is missing required fields or has wrong types will pass linting but fail validation.
Validate and format JSON instantly with the JSON Formatter on WizlyTools.