Skip to main content

JSON Structure & Data Types

In this lesson, you will learn the fundamental data types allowed in JSON and how Python maps its native types to JSON.


1. Supported JSON Data Types

JSON supports 6 core data types:

JSON Data TypePython EquivalentExample
Objectdict (Dictionary){"name": "Sai", "age": 25}
Arraylist (List)["Python", "Machine Learning"]
Stringstr (Text)"Hyderabad"
Numberint or float100 or 98.6
Booleanbooltrue or false
NullNonenull

2. Nested JSON: Real-World Example

In real-world applications (like e-commerce apps or AI APIs), JSON objects are rarely flat. They contain nested objects and arrays inside objects:

{
"order_id": "ORD-98421",
"customer": {
"name": "Ananya Sharma",
"city": "Vijayawada",
"is_prime_member": true
},
"items": [
{"product": "Wireless Mouse", "price": 499, "qty": 1},
{"product": "USB-C Cable", "price": 199, "qty": 2}
],
"discount_code": null,
"total_amount": 897.00
}

Notice:

  1. customer is a nested JSON object (maps to a nested Python dictionary).
  2. items is a JSON array of objects (maps to a Python list of dictionaries).
  3. discount_code is null (maps to Python None).

3. How to Navigate Nested JSON in Python

Once this JSON is converted into a Python dictionary, you navigate it using standard dictionary and list bracket notation:

order = {
"order_id": "ORD-98421",
"customer": {"name": "Ananya Sharma", "city": "Vijayawada"},
"items": [
{"product": "Wireless Mouse", "price": 499},
{"product": "USB-C Cable", "price": 199}
]
}

# 1. Access customer name:
print(order["customer"]["name"]) # Output: Ananya Sharma

# 2. Access the first item's product name:
print(order["items"][0]["product"]) # Output: Wireless Mouse

Quick Summary

  • JSON Objects ({}): Unordered sets of key-value pairs matching Python dictionaries.
  • JSON Arrays ([]): Ordered lists of values matching Python lists.
  • Primitive Types: Strings (double-quoted), numbers (integer/float), booleans (true/false), and null.
  • Deep Nesting: Objects and Arrays can nest arbitrarily to represent complex hierarchical data models (like e-commerce orders or REST responses).

What's Next?

Now let's learn how to read and write JSON strings and disk files using Python's json module in Reading & Writing JSON!