Concepts / Parsing JSON in Python

Parsing JSON in Python

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.

  • Programming

From Text to Data

When Python receives JSON from a web API or reads it from a file, the JSON begins as data written in a text format. Parsing turns that JSON into Python data structures that can be worked with directly. The important transformation is not merely reading characters: a JSON object becomes a Python dictionary, and a JSON array becomes a Python list.

parse objectpreserve nested arrayJSON textobject with an arrayPython dictionarykey-value pairsPython listordered values
What happens to a JSON text string as Python parses it into usable data structures?

JSON Origins and Structure

JSON stands for JavaScript Object Notation. It was inspired by JavaScript, but its design was influenced by Python as well. In particular, Python's dictionaries and lists helped shape the way JSON represents data. That is why JSON often feels familiar to someone who already knows Python.

The resemblance is structural. JSON objects use curly braces and key-value pairs, like Python dictionaries. JSON arrays use square brackets and ordered values, like Python lists. This similarity makes JSON easier to work with in Python and reduces the amount of new structure a Python learner has to understand.

JSON structurePython structureShared idea
JSON objectPython dictionaryKey-value pairs inside curly braces
JSON arrayPython listOrdered values inside square brackets
maps toJSON objectname: valuePython dictionarykey: value
How does a JSON object map its key-value pairs to a Python dictionary?

Objects, Arrays, and Nesting

Tracing a Nested Structure

Consider a JSON object containing a name value and an array of two interests. What Python structures should result after parsing?

Identify the outer structure: The outer JSON structure is an object, so it maps to a Python dictionary.

Identify the nested structure: The interests value is an array, so that nested part maps to a Python list.

Preserve the arrangement: The dictionary keeps the key-value relationship, while the list keeps the order of the two array values.

The parsed result is a Python dictionary containing a Python list. JSON objects can contain arrays, and arrays can contain objects, so nesting can continue in either direction.

maps tomaps to0first value0first value1second value1second value
How does a JSON array map to an ordered Python list, including the position of each value?

Nesting is one of the most useful parts of the mapping. An object can contain an array, an array can contain an object, and these structures can continue to be nested. After parsing, the same overall arrangement is represented with dictionaries and lists inside one another.

Structural Data Types

A JSON object is a collection of key-value pairs written with curly braces. It maps to a Python dictionary. JSON object keys must always be strings.

A JSON array is an ordered collection of values written with square brackets. It maps to a Python list, and the values can be of multiple types.

JSON value structurePython equivalentImportant property
ObjectDictionaryKey-value pairs; JSON keys are strings
ArrayListValues remain ordered

Structural JSON types and the Python structures they map to

Mistakes Beginners Make

  • Treating every parsed JSON value as a Python dictionary

    JSON arrays map to Python lists, not dictionaries.

    Fix: Identify whether the outer structure is an object or an array before deciding which Python structure represents it.

  • Assuming that JSON object keys can use any kind of key

    JSON object keys must always be strings.

    Fix: Treat JSON object keys as strings when reasoning about the resulting Python dictionary.

  • Ignoring order in an array

    JSON arrays are ordered collections, and they map to ordered Python lists.

    Fix: Preserve the position of each array value when following the parsed result.

  • Assuming nesting changes the basic mapping

    JSON objects can contain arrays, and arrays can contain objects.

    Fix: Apply the same mapping at each level: object to dictionary and array to list.

Practice the Mapping

MEDIUM

A JSON structure has an outer array. Its first value is an object, and that object contains another array. Describe the Python structure after parsing, including the Python type corresponding to each level.

Hints
  • Start with the outermost structure.
  • Map each object to a dictionary and each array to a list.
  • Describe the structures from the outside inward.

What do you think happens?

What Python structure should represent a JSON object whose value is an array?

  • A Python list containing a dictionary
  • A Python dictionary containing a list
  • A Python list containing only strings
Reveal answer

Answer: A Python dictionary containing a list

The outer JSON object maps to a Python dictionary, and its nested JSON array maps to a Python list.

Key Takeaways

  1. JSON means JavaScript Object Notation. It was inspired by JavaScript and influenced by Python's dictionary and list syntax.
  2. A JSON object uses key-value pairs in curly braces and maps to a Python dictionary.
  3. A JSON array uses square brackets for an ordered collection and maps to a Python list.
  4. Nested JSON objects and arrays become nested Python dictionaries and lists.
  5. JSON's similarity to Python structures makes parsed data more familiar and easier to manipulate in Python.

Key Takeaways

  • Parsing JSON changes JSON text into usable Python data structures.
  • JSON objects map to Python dictionaries, while JSON arrays map to Python lists.
  • JSON preserves nested relationships between objects and arrays during this structural mapping.
  • JSON's design was inspired by JavaScript and influenced by Python's dictionaries and lists.
  • Recognizing the outer structure and then tracing nested structures is the key skill.