Working with Python Dictionaries
JSON (JavaScript Object Notation) was inspired by JavaScript but influenced by Python's dictionary and list syntax, making it structurally similar to Python data structures.
A Familiar Data Format
JSON stands for JavaScript Object Notation. It was inspired by JavaScript, but its design was also influenced by Python's dictionaries and lists. That history explains why JSON often feels familiar to someone who already understands Python collection syntax.
JSON is a data format whose structure closely resembles Python dictionaries and lists.
Objects Become Dictionaries
A JSON object is a collection of key-value pairs written with curly braces. It maps naturally to a Python dictionary, which also uses key-value pairs inside curly braces. The key difference emphasized here is that JSON keys must always be strings.
Reading an Object as a Dictionary
Consider the JSON-shaped data {"name": "Ada", "role": "engineer"}. What Python structure does its outer shape resemble?
Find the braces: The outer curly braces indicate an object-like collection of key-value pairs.
Find the keys: The labels name and role are keys, and both are strings.
Find the values: Ada and engineer are the values associated with those keys.
Map the structure: An object made of key-value pairs maps to a Python dictionary.
The outer JSON object corresponds structurally to a Python dictionary with string keys.
Arrays Become Lists
A JSON array is an ordered collection written with square brackets. It maps naturally to a Python list, which is also an ordered collection written with square brackets. Both structures can contain values of multiple types.
Preserving Array Order
Consider the JSON-shaped data ["red", "green", "blue"]. What Python collection matches its outer structure?
Find the brackets: The square brackets indicate an array-like collection.
Observe the sequence: The items appear in a specific order: red, then green, then blue.
Map the structure: An ordered collection in square brackets maps to a Python list.
The array corresponds structurally to a Python list whose item order is red, green, blue.
Reading the Shared Syntax
The resemblance is easiest to see in the punctuation. Curly braces group key-value pairs in objects and dictionaries. Square brackets group ordered items in arrays and lists. Because these collection shapes are similar, a nested JSON structure can feel like a familiar combination of Python dictionaries and lists.
Nesting does not change the basic mapping. An object can contain an array, an array can contain an object, and this pattern can continue. When reading nested JSON in Python, identify the outermost collection first, then inspect the collection inside it.
Nested Data in Practice
Mapping a Nested Structure
Consider the JSON-shaped data {"person": {"name": "Mina"}, "skills": ["Python", "JSON"]}. Trace the outer and inner structures.
Read the outer braces: The outer structure is an object, so it maps to a Python dictionary.
Read the person value: The value associated with person is another object, so that nested value maps to another Python dictionary.
Read the skills value: The value associated with skills is an array, so that nested value maps to a Python list.
Combine the mappings: The resulting Python-shaped structure is a dictionary containing a dictionary and a list.
Objects map to dictionaries at both object levels, while the array maps to a list. The nesting relationship is preserved.
Treating every JSON collection as a dictionary
Square brackets indicate an ordered array-like collection, not a key-value object.
Fix:
Map a JSON array to a Python list.Treating every JSON collection as a list
Curly braces with key-value pairs indicate an object.
Fix:
Map a JSON object to a Python dictionary.Assuming JSON keys can use any kind of key
JSON keys must always be strings.
Fix:
Check that each JSON key is represented as a string.Ignoring nesting
The inner object and array have different collection roles even though both are inside the outer object.
Fix:
Trace each nested value separately: object to dictionary and array to list.
Practice the Mapping
For each structure, identify the corresponding Python collection shape: {"course": "Programming"}, ["dictionary", "list"], and {"topics": ["JSON", "Python"]}. Then explain what the curly braces or square brackets tell you about the structure.
Hints
- Curly braces signal an object-like key-value collection.
- Square brackets signal an ordered collection.
- For the nested structure, identify the outer collection and then the inner collection.
What do you think happens?
Before checking the answer, predict the Python collection shape for {"items": ["pen", "book"]}.
Reveal answer
Answer: A dictionary containing a list
The outer curly braces indicate an object that maps to a Python dictionary. The value for items is inside square brackets, so that nested value maps to a Python list.
Key Takeaways
- JSON means JavaScript Object Notation and was inspired by JavaScript.
- Python dictionaries and lists also influenced JSON's design, which is why the structures look familiar.
- A JSON object uses key-value pairs in curly braces and maps to a Python dictionary.
- A JSON array is an ordered collection in square brackets and maps to a Python list.
- Objects and arrays can be nested, so dictionaries can contain lists and other dictionaries when representing JSON-shaped data.
Key Takeaways
- JSON is a data format inspired by JavaScript and influenced by Python's dictionary and list syntax.
- JSON objects and Python dictionaries both organize data as key-value pairs in curly braces.
- JSON arrays and Python lists both represent ordered collections in square brackets.
- Nested JSON structures preserve the same object-to-dictionary and array-to-list relationships.
- JSON keys must always be strings.