Concepts / Reading JSON from Files with json.load()

Reading JSON from Files with json.load()

json.loads() transforms a JSON string into a Python data structure by parsing the text and building corresponding lists, dictionaries, and primitive values.

  • Programming

From Text to Usable Data

A JSON response may begin as ordinary text, but your Python program usually needs a list, dictionary, or primitive value that it can work with. The json.loads() function parses a JSON string and builds a corresponding Python data structure. The original string remains unchanged, so assign the returned Python object to a variable.

inputbuildscontainskey nameJSON string[{"id": 1, "name":"Chuck"}]json.loads(data)parse the textPython listouter [ ]Python dictionaryinner { }Chuckname value
What happens to JSON text as json.loads() parses its outer container, nested objects, and values?

Parsing the Outer Structure

json.loads(data) reads the JSON string and builds a new Python object. The outer square brackets in the JSON text indicate a list. Curly braces inside that list indicate dictionary objects. Colons separate keys from values, while commas separate elements. After parsing a JSON string representing two users, the result is a Python list containing two dictionaries.

python
Output
<class 'str'>
<class 'list'>
2
Chuck

The variable data still refers to the original JSON string. The variable info refers to the new Python object returned by json.loads(). Because the outer JSON value is a list, len(info) counts the two user dictionaries. The expression info[0] selects the first dictionary, and info[0]["name"] selects the value associated with its name key.

Following Nested Access

index [0]selectskey [name]returnsinfolist0first dictionaryuser dictionaryid, x, namenamekeyChuckvalue
How do you move through the parsed list and dictionary to retrieve the first user's name?

Reading the First User's Name

Given a parsed value named info that contains a list of user dictionaries, retrieve the name of the first user.

Start with info: The parsed result is a list containing user dictionaries.

Use index 0: info[0] selects the first dictionary in the list.

Use the name key: info[0]["name"] looks up the name field in that dictionary.

The expression info[0]["name"] returns Chuck for the source example.

first_user = info[0] user_name = first_user["name"] print(user_name)

Knowing the Expected Shape

Parsing JSON does not tell you which path you intended to follow. You need to know the structure in advance: whether the outer value is a list or dictionary, whether a list index is required, and which keys exist in each dictionary. In the user example, you must know that the result is a list of users, that each user is a dictionary, and that the dictionaries contain keys such as id, x, and name.

indexed bylooked up byPython listuse an index0first elementPython dictionaryuse a keynamefield name
Why must you identify the container type and required key or index before accessing a parsed value?
inputinputreturnsreturnsJSON stringin memoryjson.loads()string parserJSON filefile sourcejson.load()file readerPython objectparsed structure
How does the input source named by each function differ when working with JSON?

Tracing Parsing Problems

When parsing or access produces an unexpected result, inspect the process in layers. First verify that the input is a string containing valid JSON. Next confirm the structure of the parsed result. Finally check that every index and key matches that structure. Printing each layer makes it easier to identify where your expectation and the actual data diverged.

python
  • Forgetting to import json

    The name json has not been made available to the program.

    Fix: Import the json module before calling json.loads().

  • Passing a non-string object to json.loads()

    json.loads() is used to parse a JSON string, not an already parsed Python object.

    Fix: Pass the JSON string to json.loads(), then assign its returned Python object to a separate variable.

  • Using a field that does not exist

    The source structure contains fields such as id, x, and name; accessing an absent field can produce a KeyError.

    Fix: Check the documented structure and use the exact key that exists.

  • Skipping the structural trace

    You cannot reliably navigate the value if you do not know whether each layer is a list or dictionary.

    Fix: Print the type and contents of each relevant layer before changing the access expression.

Practice the Transformation

EASY

Suppose data contains a JSON string representing a list of two user dictionaries. Each dictionary has the keys id, x, and name, and the first user's name is Chuck. Write Python statements that parse data, store the returned object in info, count the users, and retrieve the first user's name.

Hints
  • Import the json module.
  • Assign json.loads(data) to info.
  • Use len(info) to count the outer list elements.
  • Use info[0]["name"] to reach the first user's name.

What do you think happens?

Before running the access expression, what will info[0]["name"] return when info is the parsed user list from the source example?

  • The entire list
  • The first user dictionary
  • Chuck
  • The number of users
Reveal answer

Answer: Chuck

The index 0 selects the first dictionary, and the name key selects that dictionary's name value.

Key Takeaways

  1. json.loads() parses a JSON string and returns a new Python object.
  2. The original JSON string is unchanged, so store the returned object in a variable.
  3. JSON arrays become Python lists in the parsed structure, while JSON objects become Python dictionaries.
  4. Navigate lists with indices and dictionaries with keys, following the structure documented for the data.
  5. When debugging, inspect the input, parsed type, length, intermediate values, and final key or index access.

Key Takeaways

  • json.loads() converts JSON text into a new Python data structure without changing the original string.
  • Square brackets represent the outer list in the user example, while curly braces represent dictionaries inside that list.
  • Nested access follows the parsed shape: info[0] selects the first list element and info[0]["name"] selects its name value.
  • You must know the expected structure and field names before navigating parsed JSON effectively.
  • A step-by-step trace of types, lengths, intermediate values, keys, and indices helps diagnose parsing and access errors.