JSON Formatting and Validation: Fix Errors Fast

Understand JSON syntax rules, the most common parse errors, how to validate with JSON Schema, and when to choose YAML or TOML instead.

Last updated · 2026-09-04

JSON's strict rules

JSON looks like JavaScript but is much stricter. Keys must be in double quotes. Strings use double quotes only. No trailing commas, no comments, and no undefined, NaN or Infinity. Values are limited to strings, numbers, booleans, null, arrays and objects.

Reading parse errors

'Unexpected token } in JSON at position 142' usually means a trailing comma just before that brace. 'Unexpected token ' ' points to single quotes. 'Unexpected end of JSON input' means the document was truncated — often a network response cut short or an empty body. Our JSON validator highlights the exact line and column.

Formatting for humans and machines

Pretty-printed JSON with 2-space indentation is ideal for config files and code review. Minified JSON removes all whitespace for network payloads. Both represent identical data.

JSON.stringify(data, null, 2); // pretty
JSON.stringify(data);          // minified

Validating structure with JSON Schema

Syntax validity is not enough: an API also needs the right fields and types. JSON Schema describes the expected shape, and libraries like Ajv (JavaScript) or jsonschema (Python) enforce it. In TypeScript, generate interfaces from a sample response with our JSON to TypeScript tool and validate at runtime with Zod.

JSON vs YAML vs TOML

JSON is best for machine-to-machine data. YAML is popular for human-edited configs such as Docker Compose and Kubernetes, supports comments, but is whitespace-sensitive. TOML is explicit and simple, used by Cargo and pyproject.toml. Our converters move data between all three in one click.

Security tips

Never use eval() to parse JSON — always JSON.parse. Limit request body size on servers to prevent memory exhaustion, and be careful when merging parsed objects into existing ones: keys like __proto__ can cause prototype pollution.

Try the tools from this article

← All tutorials