JSON syntax: objects, arrays and types
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A shop's website, its mobile app and a courier's system are written in different languages, yet they must exchange orders. They need one text format all of them read, and one wrong character can break it.
Objects, arrays and the six value types
JSON (JavaScript Object Notation) is a plain-text format for structured data, built from objects, arrays and six value types. Almost every web API sends its requests and responses as JSON.
One customer record uses every JSON value type. Read it once as a whole, then see how each key and value is written.
Example 1 · customer.json: all six value types
{"customerId":1,"fullName":"Asha Rao","email":"asha.rao@example.com","phone":"9876543201","loyaltyPoints":120,"primeMember":false,"referredBy":null,"tags":["student","hostel"],"defaultAddress":{"city":"Bengaluru","pincode":"560038"}}{
"customerId": 1,
"fullName": "Asha Rao",
"email": "asha.rao@example.com",
"phone": "9876543201",
"loyaltyPoints": 120,
"primeMember": false,
"referredBy": null,
"tags": [
"student",
"hostel"
],
"defaultAddress": {
"city": "Bengaluru",
"pincode": "560038"
}
}What this code does
- APIs usually send JSON on one line to save bytes. The validator prints the same data pretty: one pair per line, nested values indented.
customerIdandloyaltyPointsare numbers,fullNamea string,primeMembera boolean,referredByisnull,tagsan array anddefaultAddressan object.phoneandpincodeare strings: they are identifiers, not amounts, and nobody adds two phone numbers.
A JSON object is a list of name-value pairs inside curly braces: {"city": "Pune"}. Each name (also called a key) is text in double quotes, followed by a colon and a value, and pairs are separated by commas. A JSON array is an ordered list of values inside square brackets: ["student", "hostel"].
A value is one of exactly six types:
- string: text in double quotes,
"Asha Rao" - number:
120,-5,0.18,2.5e3(no quotes) - boolean and null:
true,falseandnull, always lower case - object and array: values can hold other objects and arrays
python -m json.tool, prints for each document. The last section explains the tool.Rules that break JSON
Valid JSON follows the strict rules of the JSON standard (RFC 8259). It looks like a JavaScript object but is stricter: one wrong character makes the whole document unreadable, and the receiver stops at the first error.
Two small documents each break one rule. The validator stops at the first error and says where.
Example 2 · single-quotes.json: the validator stops at the first quote
{ 'sku': 'MOB-RN13', "name": "Redmi Note 13 5G", "stock": 40}Expecting property name enclosed in double quotes: line 2 column 3 (char 4)
What this code does
- Line 2, column 3 is the
'beforesku. The parser wanted"to start a key and found a single quote instead. - The fix is
"sku": "MOB-RN13". The validator reports only the first error, so fix it and run the check again.
Example 3 · trailing-comma.json: a comma after the last item
{ "sku": "ACC-AD141", "name": "boAt Airdopes 141", "colours": ["black", "blue", "white",], "stock": 120}Illegal trailing comma before end of array: line 4 column 39 (char 93)
What this code does
- JavaScript and Java array initialisers allow a comma after the last item; JSON does not.
- Column 39 of line 4 is the
]right after the extra comma. Remove the comma after"white"and the document is valid.
The four mistakes beginners make most often:
- Single quotes:
'Pune'is wrong; strings and keys need double quotes,"Pune". - Trailing commas:
[1, 2, 3,]and{"a": 1,}are wrong; no comma after the last item. - Comments:
// ...and/* ... */are not allowed anywhere in JSON. - Unquoted keys:
{city: "Pune"}is wrong; every key is a quoted string.
Find the bug
Find the line that is not valid JSON
Select the line that is wrong.
One line breaks a JSON rule. Tap it.
Round 1 of 4True, None or undefined (only true, false, null), numbers with leading zeros such as 007, and a line break typed inside a string (write \n instead).Nesting: an order with its customer and items
Nesting means putting an object or array inside another one. One thing becomes an object, many things become an array of objects.
ShopLite order 14 has a customer, an address and a list of items. Look at how each one is nested.
Example 4 · order.json: a nested ShopLite order
{ "orderId": 14, "status": "SHIPPED", "orderedAt": "2026-09-21T21:05:00+05:30", "customer": { "customerId": 2, "fullName": "Ravi Kumar", "email": "ravi.kumar@example.com" }, "shippingAddress": { "line1": "7 Anna Salai, T Nagar", "city": "Chennai", "pincode": "600017" }, "items": [ { "sku": "MOB-RN13", "name": "Redmi Note 13 5G", "quantity": 1, "unitPrice": 17999.00 }, { "sku": "ACC-AD141", "name": "boAt Airdopes 141", "quantity": 1, "unitPrice": 1299.00 } ]}{
"orderId": 14,
"status": "SHIPPED",
"orderedAt": "2026-09-21T21:05:00+05:30",
"customer": {
"customerId": 2,
"fullName": "Ravi Kumar",
"email": "ravi.kumar@example.com"
},
"shippingAddress": {
"line1": "7 Anna Salai, T Nagar",
"city": "Chennai",
"pincode": "600017"
},
"items": [
{
"sku": "MOB-RN13",
"name": "Redmi Note 13 5G",
"quantity": 1,
"unitPrice": 17999.0
},
{
"sku": "ACC-AD141",
"name": "boAt Airdopes 141",
"quantity": 1,
"unitPrice": 1299.0
}
]
}What this code does
- One customer and one address are objects; the two items are an array of objects with the same four keys each. This is order 14 from the ShopLite database.
- Look at the prices:
17999.00came back as17999.0. Python read them as doubles. The value is still equal here, but the next section shows when it is not.
The rule of thumb: one thing becomes an object ("customer": {...}), many things become an array of objects ("items": [{...}, {...}]). The order's own facts, such as its id and status, stay as simple values at the top level.
Numbers and money, dates as ISO-8601 strings
ISO-8601 is the standard way to write dates and times as text: 2026-09-24 for a date, 2026-09-21T21:05:00+05:30 for a moment with India's offset from UTC, and a trailing Z for UTC.
One document holds a price and three dates. It shows what survives the trip through a reader and what does not.
Example 5 · money-and-dates.json: numbers versus strings
{ "priceAsNumber": 17999.00, "priceAsString": "17999.00", "yearlyUpiVolumeAsNumber": 198765432109876.54, "yearlyUpiVolumeAsString": "198765432109876.54", "gstRate": 0.18, "deliveryDate": "2026-09-24", "orderedAt": "2026-09-21T21:05:00+05:30", "paidAt": "2026-09-21T15:35:12Z"}{
"priceAsNumber": 17999.0,
"priceAsString": "17999.00",
"yearlyUpiVolumeAsNumber": 198765432109876.53,
"yearlyUpiVolumeAsString": "198765432109876.54",
"gstRate": 0.18,
"deliveryDate": "2026-09-24",
"orderedAt": "2026-09-21T21:05:00+05:30",
"paidAt": "2026-09-21T15:35:12Z"
}What this code does
- The large amount as a number changed from
.54to.53: a double cannot hold that many digits, so one paisa was lost on the way through. The string copy is exact. - A reader that uses exact decimals (Jackson with
BigDecimal, as the coming lessons do) keeps the number form exact too, so both styles work when both sides agree. - The three dates are ISO-8601 strings: a plain date, a time with the
+05:30offset for India, and a UTC time ending inZ.
JSON has one number type with no size limit, but the program that reads it decides how to store it. Many readers, including JavaScript and Python, turn every decimal number into a binary double, which keeps only about 15-17 significant digits and cannot store most decimal fractions exactly. A price written as 17999.00 may come back as 17999.0, and a very large amount can lose its last paisa.
So APIs send money as an exact decimal number that the receiver reads into an exact type (in Java, BigDecimal), or as a string such as "17999.00". Identifiers that only look like numbers, such as phone numbers, PIN codes and PNRs, are strings too. JSON has no date type, so dates travel as ISO-8601 strings; text like "21/09/2026" is ambiguous (day or month first?) and is best avoided.
Validating and pretty-printing JSON, and reading a parse error position
Pretty-print means laying JSON out with one value per line and indentation, so people can read it. A JSON validator reads a document and either confirms it is valid or reports the first error with its position.
Python's json.tool validates a file and pretty-prints it. Use python on Windows and python3 on macOS; both lines below do the same job.
python -m json.tool --indent 2 order.jsonpython3 -m json.tool --indent 2 order.json- Code editors such as VS Code and IntelliJ IDEA underline JSON errors in
.jsonfiles as you type. - Online validators work too, but never paste real customer data or keys into a website.
Example 6 · missing-comma.json: reading the error position
{ "orderId": 15, "status": "PLACED", "items": [ { "sku": "LAP-IPS3", "quantity": 1 } { "sku": "ACC-M331", "quantity": 2 } ]}Expecting ',' delimiter: line 6 column 5 (char 99)
What this code does
- Line 6, column 5 is the
{of the second item. The parser finished the first item on line 5 and expected a,or], but met{. - The missing comma belongs at the end of line 5, one line before the reported position. Add it and the document is valid.
An error such as Expecting ',' delimiter: line 6 column 5 (char 99) gives the line (counting from 1), the column (characters from the start of that line, counting from 1) and the char offset (characters from the start of the file, counting from 0). The real mistake is usually just before that position, because the parser only notices a missing comma when it meets the next value.
Fix the error
Fix the line the validator reports
Read the error, then pick the line that fixes it.
Round 1 of 2
Fix the line the validator points to.
Error
Illegal trailing comma before end of array: line 4 column 39 (char 93)
Next step
Check your understanding
12 quick questions