JSON Syntax Rules — The Strict Basics
JSON is notoriously strict about syntax. Unlike JavaScript itself, JSON does not allow: - **Trailing commas** — `{"key": "value",}` is invalid JSON - **Single quotes** — All strings must use double quotes: `"string"`, never `'string'` - **Unquoted keys** — Object keys must always be strings: `{"name": "John"}`, not `{name: "John"}` - **Comments** — JSON has no comment syntax (use JSONC/JSON5 if you need comments) - **undefined, NaN, Infinity** — These JavaScript values are not valid JSON types - **Trailing content** — A valid JSON document is a single value (object, array, string, number, boolean, or null)
Common JSON Errors and How to Fix Them
**SyntaxError: Unexpected token '}' at position 45** This usually means a trailing comma. Look for `,}` or `,]` patterns. **SyntaxError: Unexpected token 'u' at position 0** The value `undefined` was serialized. In JavaScript, `JSON.stringify(undefined)` returns `undefined` (not the string "undefined"), which breaks downstream JSON.parse(). **SyntaxError: Unexpected end of JSON input** The JSON is truncated — often caused by network errors, file corruption, or an incomplete API response. **The "keys must be strings" error** JavaScript objects can have numeric keys, but JSON cannot. Always wrap numbers in quotes when converting JS to JSON.
Prettify vs Minify — When to Use Each
**Prettified JSON** uses consistent indentation (typically 2 or 4 spaces) to make the structure human-readable. Use this for: - Documentation and README files - Debugging API responses - Config files checked into version control **Minified JSON** removes all whitespace, reducing file size. Use this for: - API responses in production (smaller payload = faster transfer) - Embedded JSON in JavaScript bundles - Storage in databases where whitespace is wasted space Our [JSON Formatter](/json-viewer) tool handles both directions instantly — paste minified JSON to prettify it, or click Minify to compress prettified JSON.
JSON Schema — Validate Your Data Structure
JSON Schema is a vocabulary for validating the structure of JSON data. It lets you define what fields are required, what types they must be, and what values are acceptable. This is how frameworks like OpenAPI/Swagger define their API contracts. Use our [JSON Schema Generator](/json-schema-generator) to automatically generate a JSON Schema from any sample JSON object. The resulting schema can then be used with validators like `ajv` in Node.js or `jsonschema` in Python to validate real API responses.
Conclusion
Mastering JSON formatting saves hours of debugging time. Bookmark our [JSON Formatter & Validator](/json-viewer) for instant prettification, and use the [JSON Diff tool](/json-diff) whenever you need to compare two API responses or config file versions. Clean, valid JSON is the foundation of reliable software.