JSON Data Representation
JSON (JavaScript Object Notation) is a text format for representing structured data, and it is the standard way web APIs send and receive information. It looks almost exactly like JavaScript object literals, which makes it easy to read, but it has stricter rules. Any language, from Python to Java, can parse it, so it works as a common language between frontends and backends.
The six value types
JSON supports only a small set of value types:
- String: always in double quotes, such as
"Asha". - Number: integers or decimals like
42or3.5; no leading zeros, noNaNorInfinity. - Boolean:
trueorfalse. - Null:
nullfor "no value". - Object: key-value pairs in curly braces, where every key is a double-quoted string.
- Array: an ordered list of values in square brackets.
Objects and arrays can be nested inside each other to any depth, which is how complex data such as an order with a customer and a list of items is represented.
{
"orderId": 1087,
"paid": true,
"coupon": null,
"customer": {
"name": "Asha Verma",
"email": "asha@example.com"
},
"items": [
{ "sku": "LAMP-01", "qty": 1, "price": 899.5 },
{ "sku": "BULB-04", "qty": 3, "price": 120 }
]
}
How JSON differs from a JavaScript object
| Rule | JavaScript object | JSON |
|---|---|---|
| Keys | Quotes optional | Double quotes required |
| Strings | Single, double or backtick quotes | Double quotes only |
| Trailing commas | Allowed | Not allowed |
| Comments | Allowed | Not allowed |
| Functions, undefined, dates | Allowed | Not supported |
Because JSON has no date type, dates are usually sent as ISO 8601 strings in the pattern "YYYY-MM-DDTHH:mm:ssZ", produced by date.toISOString(), and converted back with new Date(value) after parsing.
Converting between JSON and JavaScript
JSON.stringify(value) turns a JavaScript value into a JSON string, ready to send in a request body. Pass extra arguments, as in JSON.stringify(data, null, 2), to get indented, readable output. JSON.parse(text) does the reverse. When using fetch, await response.json() parses the body for you, and when sending you set the Content-Type: application/json header so the server knows what it is receiving.
Errors you will run into
"Unexpected token" errors from JSON.parse almost always mean the text is not valid JSON. Typical causes are single quotes, a trailing comma after the last item, or, very often, the server returning an HTML error page instead of JSON. If you see "Unexpected token <", the first character was the start of an HTML tag; check the network tab to see what the server actually sent.
Another surprise is that JSON.stringify silently drops properties whose value is undefined or a function, and turns NaN into null. Very large integers can also lose precision when parsed, since JavaScript numbers are floating point, so IDs beyond the safe integer range are best sent as strings.
Designing clear JSON
Pick one naming style, usually camelCase, and stick to it. Use arrays for lists even when there is only one item, so clients do not have to check the type. Use null deliberately for "known to be empty" rather than leaving fields out at random.
Takeaway: JSON uses six value types with double-quoted keys and strings, has no comments, trailing commas or dates, and is converted with JSON.stringify and JSON.parse.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.