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 Type | Python Equivalent | Example |
|---|---|---|
| Object | dict (Dictionary) | {"name": "Sai", "age": 25} |
| Array | list (List) | ["Python", "Machine Learning"] |
| String | str (Text) | "Hyderabad" |
| Number | int or float | 100 or 98.6 |
| Boolean | bool | true or false |
| Null | None | null |
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:
customeris a nested JSON object (maps to a nested Python dictionary).itemsis a JSON array of objects (maps to a Python list of dictionaries).discount_codeisnull(maps to PythonNone).
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), andnull. - 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!