JSON Basics: How to Read, Format and Validate JSON

JSON, short for JavaScript Object Notation, is the most common format for sending data between websites and servers. It is easy for people to read and for programs to parse.
The building blocks
JSON has just six kinds of value:
- Object: a set of key-value pairs in curly braces:
{"name": "Ayesha", "age": 29} - Array: an ordered list in square brackets:
[1, 2, 3] - String: text in double quotes
- Number: integers or decimals, without quotes
- Boolean:
trueorfalse - null: an intentional empty value
Values can be nested, so an object can contain arrays of objects, which is how real-world data is structured.
Strict syntax rules
- Keys must be strings in double quotes. Single quotes are invalid.
- Items are separated by commas, but there must be no trailing comma after the last item.
- No comments are allowed.
- There is no
undefined, and no functions or dates; dates are usually stored as strings (for example ISO 8601:2026-10-10T09:30:00Z). - Special characters inside strings must be escaped, such as
\nfor a newline and\"for a quote.
Why formatting helps
Servers often send JSON as one long line to save bytes. A formatter adds line breaks and indentation, making the structure visible. The reverse, minifying, removes whitespace to reduce the payload.
The most common errors
- Trailing commas.
- Missing quotes around keys.
- Mismatched brackets, especially in deep nesting.
- Copying JavaScript object literals, which look similar but allow unquoted keys and comments.
- Invisible characters from copy-paste, such as smart quotes.
A debugging method
- Paste the text into a validator and read the error message and position.
- Fix the first error only; later errors are often caused by it.
- Format the result and check that the structure matches what you expect.
- Compare with a known good sample.
Working with numbers
Very large integers can lose precision when parsed by JavaScript. If an ID has more than 15 digits, send it as a string. Also remember that JSON does not distinguish integers from decimals.
Converting to other formats
Arrays of flat objects convert neatly to CSV for spreadsheets. Deeply nested data does not, and may need flattening choices (for example address.city).
Security
Never use eval() to parse JSON. Use JSON.parse() or your language's safe parser, and validate the data's shape before trusting it.
Good JSON habits save a surprising amount of debugging time.


