Format, validate, and minify JSON.
JSON Guide
JSON (JavaScript Object Notation) is a lightweight data exchange format that has become a standard in modern programming.It's easy for humans to read and write, and easy for machines to parse and generate. JSON is programming language-independent, making it ideal for communication between different systems and web applications.
JSON structure is based on key-value pairs and ordered lists of values.JSON objects are enclosed in curly braces {} and contain zero or more key:value pairs, separated by commas.JSON arrays are enclosed in square brackets [] and contain an ordered list of values.Values can be strings (in quotes), numbers, booleans (true/false), null, objects, or arrays.
Formatting JSON improves code readability.Adding indentation and newlines makes the data structure more transparent, which is invaluable during debugging and data analysis. Minification (removing whitespace) reduces file size, which is important for web application performance, especially when transmitting data over networks.
JSON validation is a crucial step before processing data.Invalid JSON can cause application errors and unexpected behavior. Common issues include missing commas, incorrect quotes, trailing commas in lists, or using single quotes instead of double quotes. Validation tools help quickly identify and fix errors.
Results are for informational purposes. Verify results with other sources.
JSON with a repeated key parses cleanly and throws two values away
Parse {"id": 1, "id": 2, "id": 3} and you get {"id": 3}. No error, no warning — the last occurrence wins and the earlier ones vanish. The specification permits this, every mainstream parser does it, and it means a duplicated key in a config file is not a syntax error you will be told about.
How it works
- Formats and validates JSON, reporting the position of a syntax error rather than only that one exists.
- Minifies for transport and indents for reading, which are different jobs on the same data.
- Surfaces the structural problems a parser accepts in silence.
duplicate keys → last occurrence wins, silently integers above 2⁵³ − 1 → rounded on parse, silently NaN and Infinity → null undefined in an object → key dropped undefined in an array → null BigInt → throws TypeError
Worked example
Three things a parser does to valid JSON without complaining.
- {"id": 1, "id": 2, "id": 3} → {"id": 3}
- {"id": 9007199254740993} → {"id": 9007199254740992}
- {"id": 1770000000123456789} → {"id": 1770000000123456800}
- {a: undefined, b: 1} → {"b": 1} — the key disappears
- [undefined, 1] → [null, 1] — but here it becomes null
The same undefined value is dropped inside an object and preserved as null inside an array. None of these produce an error, which is why they surface much later as a missing field or an identifier that no longer matches anything.
Reading the result
- Large identifiers are the most damaging case. A 19-digit ID passes through JSON.parse as a slightly different number, so lookups fail against a database that stored the original. Transmit such IDs as strings — this is exactly why large APIs return id and id_str side by side.
- JSON has no comments, no trailing commas and no date type. Dates survive only as ISO strings by convention, so a round trip through JSON turns a Date into text and will not turn it back without explicit handling.
- Indentation costs more than people expect: the sample here grows 84.7% when pretty-printed with two spaces. Format for humans reading it and minify for anything crossing a network, since the difference is close to double.
- Key order is preserved by every major implementation but is not guaranteed by the specification. Relying on it works until it does not, and comparing two JSON documents by string equality will report differences that are purely ordering.
Common questions
- Why did my ID change after a round trip?
- It exceeded 2⁵³ − 1 and was rounded to the nearest representable value. 1770000000123456789 comes back as 1770000000123456800. The fix is to carry large identifiers as strings, because no amount of care in the parser recovers digits already lost.
- Is a duplicate key invalid JSON?
- No. The specification allows it and parsers resolve it by keeping the last occurrence. That makes it a genuine hazard in hand-edited configuration, where a repeated key silently overrides an earlier setting and nothing reports the conflict.