Parsing JSON in Your Application
JSON uses two main structures: objects (key-value pairs in curly braces) and arrays (ordered lists in square brackets).
From Text to Data
When an application receives information from a web service, database, or another system, that information may be represented as JSON. JSON is a lightweight, text-based format for encoding and exchanging data. Its structure is built from two main forms: objects and arrays. Learning to read those forms lets you follow how information is organized before an application uses it.
The Two JSON Structures
A JSON object is a collection of key-value pairs enclosed in curly braces. An object describes named properties, such as the name, phone, or email information associated with one entity. A JSON array is an ordered list enclosed in square brackets. An array represents a collection in which the position of each item matters.
| Structure | Delimiters | Organizes | Typical purpose |
|---|---|---|---|
| Object | Curly braces | Key-value pairs | Named properties of one entity |
| Array | Square brackets | Ordered values | A collection of similar items |
The delimiters provide the first visual clue about a JSON value's structure.
Following Key-Value Pairs
A key-value pair connects a property name to the data stored for that property. In JSON, the key must be a string written in double quotes, and a colon separates the key from its value. Commas separate multiple key-value pairs inside an object.
Reading an Object
Consider this JSON object: { "name": "Avery", "phone": { "type": "mobile", "number": "555-0100" }, "email": { "hide": false } }
- 1Find the outer delimiters
The outer curly braces show that the top-level value is an object.
- 2Separate the top-level pairs
The object has three keys: name, phone, and email. Commas separate these pairs.
- 3Read simple values
The value for name is the string Avery.
- 4Inspect nested values
The values for phone and email are objects rather than simple strings. The phone object contains type and number. The email object contains hide.
The outer object represents one entity with three named properties, two of which contain additional objects.
Reading Nested Paths
Nesting means that one JSON value appears inside another. An object can contain another object or an array, and an array can contain objects. To read a deeply nested value, begin at the outer structure and follow one key or array item at a time. Each step narrows the path to the value you want.
Tracing a Nested Value
Consider this JSON object: { "profile": { "contact": { "email": "learner@example.test" } } }
- 1Start at the outer object
The outer curly braces identify the top-level object.
- 2Follow profile
The key profile leads to another object.
- 3Follow contact
Inside profile, the key contact leads to another object.
- 4Read email
Inside contact, the key email leads to the string learner@example.test.
The nested path is profile, then contact, then email. The final value is learner@example.test.
Objects Inside Arrays
Arrays are useful when the data is a collection of similar items. Because an array is ordered, its items are read according to their positions. Each item can itself be an object, which allows a collection to contain several records with the same kind of named properties.
For example, an array can hold several objects representing people. Each object may have keys such as name and email, while the array preserves the collection's order. When reading such data, first recognize the array, then inspect one object at a time and follow that object's keys.
Syntax Signals
The most useful syntax clues are the delimiters and separators. Curly braces surround objects, square brackets surround arrays, a colon separates each key from its value, and commas separate items or key-value pairs. Object keys use double-quoted strings. JSON values may be strings, numbers, booleans, null, objects, or arrays.
Common Reading Errors
Common mistake Treating every JSON value as a simple piece of text
What it looks likeAssuming the value of phone is a string even when it is an object containing type and number.
Why it failsA value can itself be an object or an array, so its internal structure must be read before its contents are interpreted.
Do this insteadInspect the value's delimiters and follow its nested keys or ordered items.
Common mistake Confusing an object with an array
What it looks likeReading curly braces as an ordered list or square brackets as named properties.
Why it failsCurly braces indicate key-value pairs, while square brackets indicate an ordered list.
Do this insteadUse the opening delimiter as the first structural clue: { begins an object and [ begins an array.
Common mistake Using an unquoted object key
What it looks likeWriting name: "Avery" instead of "name": "Avery".Why it failsJSON object keys must be strings in double quotes.
Do this insteadWrite every object key inside double quotation marks.
Common mistake Losing track of the nesting path
What it looks likeLooking for email at the top level when email is inside contact, which is inside profile.
Why it failsA nested value belongs to the object or array that contains it.
Do this insteadRecord each key or array item in order from the outermost structure to the target value.
Guided Practice
Study this JSON value: { "customers": [ { "name": "Avery", "email": "a@example.test" }, { "name": "Jordan", "email": "j@example.test" } ] }. Identify the top-level structure, the value associated with customers, and the path to the email value for Jordan.
Hints
- Look at the outermost delimiters first.
- The value of customers begins with a square bracket.
- Follow customers to the second object, then follow its email key.
What do you think happens?
Before checking the explanation, decide whether the value of customers is an object or an array.
Reveal answer
Array
The value begins with a square bracket and contains ordered items. Each item in this example is an object.
The path to Jordan's email is customers, then the second item in the array, then email. The top-level value is an object because it is enclosed in curly braces. Its customers key maps to an array, and the array contains objects with named properties.
Key Takeaways
- 1Objects use curly braces and organize named key-value pairs.
- 2Arrays use square brackets and organize ordered lists of values.
- 3JSON object keys must be strings in double quotes.
- 4Values can be strings, numbers, booleans, null, objects, or arrays.
- 5To read nested JSON, start at the outer structure and follow each key or array item along the path.
At a glance
- JSON represents exchanged data with objects and arrays.
- Objects connect quoted keys to values, while arrays preserve ordered items.
- Nested objects and arrays create hierarchical data that must be followed step by step.
- Curly braces, square brackets, colons, commas, and double quotation marks provide essential syntax clues.
- Parsing allows an application to interpret JSON text as organized data.