JSON Formatting and Validation
JSON (JavaScript Object Notation) is the universal data exchange format for APIs, configuration files, and web applications. Well-formatted JSON is essential for debugging API responses, editing config files, and reviewing data from external services.
Common JSON Errors and Fixes
- Trailing commas:
{"key": "value",}— not valid JSON (valid in JS but not JSON) - Single quotes: JSON requires double quotes; single quotes cause parse errors
- Unquoted keys:
{key: "value"}— keys must be quoted strings - Missing commas: between array elements or object properties
- Unescaped chars: newlines must be \n, backslashes \\ inside strings
JSON vs JavaScript Object Literals
JSON is a strict subset of JavaScript but they're not identical. JSON keys must be double-quoted strings; JS allows unquoted keys. JSON doesn't support undefined, functions, or comments. JSON.stringify() / JSON.parse() convert between JSON strings and JS objects.
What is the difference between JSON and JSONC?
JSONC (JSON with Comments) is an extension used in VS Code's settings.json and TypeScript's tsconfig.json that allows // and /* */ comments. Standard JSON parsers reject comments — JSONC requires a specialized parser. Use strict JSON for API responses; JSONC/JSON5 for config files humans edit.
How do I minify JSON for production?
Minified JSON removes all whitespace, reducing file size. In JavaScript: JSON.stringify(data) produces minified JSON; JSON.stringify(data, null, 2) produces pretty-printed JSON with 2-space indentation. For API responses, always send minified JSON — it reduces bandwidth and parsing time.