What Is JSON? A Complete Developer Guide
JSON is a text format for exchanging structured values. An API can return a nested object that a browser reads as JavaScript data while a Python service reads the same response into dictionaries and lists. The format handles serialization. The API contract gives each field its meaning.
Read a real response
An object holds named properties, and an array holds values in order. This inventory response combines both. The item ID is a string so consumers can preserve every digit (including leading zeros). The quantity is a number because it participates in arithmetic. The explicit null means the restock date is unknown.
Property names and strings use double quotes. JSON also supports numbers, booleans and null, and any single value can appear at the root. JSON strings can escape a quote with a backslash and represent a newline as an escaped sequence.
{"warehouse":"west","items":[{"id":"000217","quantity":4,"restockOn":null}],"complete":true}Parse the body and check the contract
When reading an HTTP response, handle the status and inspect the content type before parsing. An authentication redirect or reverse-proxy error can return HTML where your client expects JSON. Once the body parses, verify required properties and types at the boundary. For this endpoint, a valid JSON string is still the wrong response.
Inside a JavaScript client, treat a parsed value as untrusted until you validate it. A type assertion can help the compiler understand a known shape, but it adds no runtime checks. When writing JSON, serialize the value with the language’s library to escape strings consistently.
const payload = JSON.parse(rawBody);
if (!Array.isArray(payload?.items)) {
throw new Error("Inventory response must include items");
}
console.log(payload.items[0]?.id);000217Troubleshoot values that change in transit
A number beyond JavaScript’s safe integer range may round during parsing. Put large identifiers into strings at the producer, before any consumer has a chance to lose precision. Keep identifiers separate from true quantities so arithmetic still behaves predictably.
Duplicate property names produce inconsistent behavior across consumers. A parser may keep the last value, while a stricter validator may reject the document. Use unique keys at the producer and test the exact payload received. Compare parsed structures when whitespace alone should be irrelevant.
Frequently asked questions
- Can JSON contain comments?
- Standard JSON has no comment syntax. JSON with comments and JSON5 are different formats and need compatible parsers.
- Is JSON Lines the same as one JSON document?
- JSON Lines stores one value per line. A parser for a single JSON document will reject a file containing several adjacent root values.