Concepts / Working with Python Dictionaries

Working with Python Dictionaries

JSON is a data format whose structure closely resembles Python dictionaries and lists.

  • Programming

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.

Key point

JSON is a data format whose structure closely resembles Python dictionaries and lists.

JSONJavaScript Object NotationJavaScriptinspirationPythondictionaries and listsinspiredinfluenced
How are JSON, JavaScript notation, and Python collection syntax related?

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.

JSON object{ key: value }namestring keynamestring keyAdavaluePython dictionary{ key: value }Adavaluecontainsmaps key tocontainsmaps key tomaps tocorresponds tocorresponds to
How does each key-value pair in a JSON object correspond to an entry in a Python dictionary?
Worked example

Reading an Object as a Dictionary

Consider the JSON-shaped data {"name": "Ada", "role": "engineer"}. What Python structure does its outer shape resemble?

  1. 1
    Find the braces

    The outer curly braces indicate an object-like collection of key-value pairs.

  2. 2
    Find the keys

    The labels name and role are keys, and both are strings.

  3. 3
    Find the values

    Ada and engineer are the values associated with those keys.

  4. 4
    Map the structure

    An object made of key-value pairs maps to a Python dictionary.

Result

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.

JSON array[ first, second ]firstposition 1firstposition 1secondposition 2Python list[ first, second ]secondposition 2contains firstcontains secondcontains firstcontains secondmaps to
How does the order of items remain visible when a JSON array is mapped to a Python list?
Worked example

Preserving Array Order

Consider the JSON-shaped data ["red", "green", "blue"]. What Python collection matches its outer structure?

  1. 1
    Find the brackets

    The square brackets indicate an array-like collection.

  2. 2
    Observe the sequence

    The items appear in a specific order: red, then green, then blue.

  3. 3
    Map the structure

    An ordered collection in square brackets maps to a Python list.

Result

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.

{object or dictionary begins[array or list beginskey: valuekey-value pairitem, itemordered values,separates entries]array or list ends}object or dictionary endscontainsfollowed byleads towardcontainsends at
Which punctuation marks reveal whether a structure is a key-value collection or an ordered collection?
Edge case

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

Worked example

Mapping a Nested Structure

Consider the JSON-shaped data {"person": {"name": "Mina"}, "skills": ["Python", "JSON"]}. Trace the outer and inner structures.

  1. 1
    Read the outer braces

    The outer structure is an object, so it maps to a Python dictionary.

  2. 2
    Read the person value

    The value associated with person is another object, so that nested value maps to another Python dictionary.

  3. 3
    Read the skills value

    The value associated with skills is an array, so that nested value maps to a Python list.

  4. 4
    Combine the mappings

    The resulting Python-shaped structure is a dictionary containing a dictionary and a list.

Result

Objects map to dictionaries at both object levels, while the array maps to a list. The nesting relationship is preserved.

  • Common mistake

    Treating every JSON collection as a dictionary

    What it looks like
    ["Python", "JSON"]
    Why it fails

    Square brackets indicate an ordered array-like collection, not a key-value object.

    Do this instead

    Map a JSON array to a Python list.

  • Common mistake

    Treating every JSON collection as a list

    What it looks like
    {"language": "Python"}
    Why it fails

    Curly braces with key-value pairs indicate an object.

    Do this instead

    Map a JSON object to a Python dictionary.

  • Common mistake

    Assuming JSON keys can use any kind of key

    What it looks like
    {"name": "Mina"}
    Why it fails

    JSON keys must always be strings.

    Do this instead

    Check that each JSON key is represented as a string.

  • Common mistake

    Ignoring nesting

    What it looks like
    {"profile": {"name": "Mina"}, "tags": ["student", "builder"]}
    Why it fails

    The inner object and array have different collection roles even though both are inside the outer object.

    Do this instead

    Trace each nested value separately: object to dictionary and array to list.

Practice the Mapping

EASY

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"]}.

  • A list containing a dictionary

  • A dictionary containing a list

  • A list containing a list

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

  1. 1
    JSON means JavaScript Object Notation and was inspired by JavaScript.
  2. 2
    Python dictionaries and lists also influenced JSON's design, which is why the structures look familiar.
  3. 3
    A JSON object uses key-value pairs in curly braces and maps to a Python dictionary.
  4. 4
    A JSON array is an ordered collection in square brackets and maps to a Python list.
  5. 5
    Objects and arrays can be nested, so dictionaries can contain lists and other dictionaries when representing JSON-shaped data.

At a glance

  • 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.

Related concepts

More concepts starting with W