Introduction to JSON: A Simpler Data Format
JSON uses two main structures: objects (key-value pairs in curly braces) and arrays (ordered lists in square brackets).
Why JSON Uses Two Structures
JSON stands for JavaScript Object Notation. It is a lightweight, text-based format for encoding and exchanging data across the web. Its central idea is simple: JSON organizes information with two main structures, objects and arrays. Objects describe named properties, while arrays represent ordered collections.
Objects and Key-Value Pairs
A JSON object is enclosed in curly braces. Inside it, information is arranged as key-value pairs. A key names a property, and its value supplies the associated data. Keys in JSON objects must be strings written in double quotes. Values may be strings, numbers, booleans, null, other objects, or arrays.
| Part | Role |
|---|---|
| Key | Names a property and must be a string in double quotes |
| Colon | Separates a key from its value |
| Value | Stores the associated data |
| Curly braces | Enclose the object |
The parts of a JSON object
Arrays and Ordered Collections
A JSON array is enclosed in square brackets. It represents an ordered list of values. The values in an array can be simple values, objects, or other arrays. Use an array when the data represents a collection of similar items rather than the named properties of one entity.
| Structure | Delimiters | Best suited for | Internal organization |
|---|---|---|---|
| Object | Curly braces | Named properties of one entity | Key-value pairs |
| Array | Square brackets | A collection of similar items | Ordered values |
Reading Nested Data
JSON structures can be nested. An object can contain another object or an array, and an array can contain objects. To read a nested structure, move from the outside inward: identify the top-level structure, find each key, and then inspect the value connected to that key. If that value is another object or array, repeat the same process.
Tracing a contact record
Consider this generated JSON structure: an outer object has the keys name, phone, and email. The value of name is the string Amina. The value of phone is an object with the keys type and number. The value of email is an object with the key hide.
Find the outer structure: The record begins as an object, so its contents are organized as named key-value pairs.
Read the simple value: The key name connects to a string value, Amina.
Enter the phone value: The key phone connects to another object. That nested object groups the related properties type and number.
Enter the email value: The key email also connects to an object. That object contains the key hide.
Describe the hierarchy: The outer object represents one contact record, while the nested objects keep phone details and email details grouped under their related keys.
The top-level object has three keys: name, phone, and email. The phone and email values are nested objects, so the structure contains related information at more than one level.
Objects Inside Arrays
Objects and arrays are not competing formats. They work together. An object can contain an array when one entity has a collection of related items. An array can contain objects when a collection contains several structured records. This combination lets JSON represent hierarchical data.
What do you think happens?
A collection contains several complete records, and each record has named properties. Which arrangement fits that description?
Reveal answer
Answer: An array containing objects
The array represents the collection, while each object represents one structured record with named properties.
Syntax Mistakes to Avoid
Treating curly braces and square brackets as interchangeable
The two structures communicate different organization: objects use key-value pairs, while arrays use ordered values.
Fix:
Choose an object for the named properties of one entity and an array for a collection of similar items.Writing an object key without double quotes
JSON object keys must be strings in double quotes.
Fix:
Represent the key as a double-quoted string.Stopping at the outermost object
A value can itself be an object or an array, so nested content must also be inspected.
Fix:
When a value is another object or array, move inward and read its contents.Assuming every JSON value is text
JSON values can have several forms, including strings, numbers, booleans, null, objects, and arrays.
Fix:
Identify the value's type as you read the key-value pair.
Practice Reading Structure
A generated data record has an outer object with a key called students. The value of students is an array. The array contains two objects, and each object has the keys name and email. Explain what the outer object represents, what the array represents, and what each inner object represents.
Hints
- Start with the outermost structure.
- Identify whether the value of students is an object or an array.
- Describe the role of each object inside the array.
Practice solution
Interpret the generated record with an outer object, a students key, an array value, and two objects inside that array.
Outer object: The outer object groups the data under named properties.
Students value: The value connected to students is an array, so it represents a collection of student items.
Array items: Each item in the array is an object, so each item is one structured record.
Inner properties: The name and email keys provide named properties within each student record.
The outer object describes a larger data structure, the students array holds a collection, and each object in that array describes one student using key-value pairs.
JSON in Data Exchange
JSON is widely encountered when data must move between systems. It has become a standard format for exchanging data across the web because it balances simplicity with expressiveness. It is easier to read and write than XML while remaining flexible enough to represent complex structures. Web APIs, databases, and other systems that send data from one place to another commonly use JSON.
The practical skill is not memorizing one sample record. It is recognizing the two structures and following their nesting. Once you can identify objects, arrays, keys, and values, you can begin interpreting much larger JSON data structures.
What to Remember
- JSON is a lightweight, text-based format for encoding and exchanging data.
- Objects use curly braces and organize information as key-value pairs.
- Arrays use square brackets and represent ordered lists of values.
- Object keys must be strings in double quotes, while values can be strings, numbers, booleans, null, objects, or arrays.
- Nesting lets objects and arrays work together to represent hierarchical data.
Key Takeaways
- JSON represents structured information with objects and arrays.
- Objects use curly braces and key-value pairs for named properties.
- Arrays use square brackets for ordered collections.
- Nested objects and arrays allow related information to be grouped into hierarchical structures.
- To read JSON, identify the current structure, follow each key to its value, and continue inward when the value is another object or array.