AppDividend
Latest Code Tutorials

Python json.load() Function Explained with Example

0

Python has an inbuilt package json, and to use json, import the json package in Python script. Using the json.load() method, you can turn JSON encoded/converted data into Python data types; this process is known as JSON decoding.

Python json.load()

Python json.load() is an inbuilt function that accepts a file object and returns the json object. A JSON object carries a key/value pair form of data.

The keys are strings, and the values are the JSON data types. Keys and values are separated by a colon. Each key/value pair is separated by a comma.

Syntax

json.load(file_object)

Parameters

It takes a file object as a parameter.

Mapping between JSON and Python entities while decoding

JSON PYTHON
object dictionary
array list
string str
number (int) int
number (real) float
true True
false False
null None

Let’s follow the below steps to use the json.load() function.

Step 1: Create a JSON file.

To working with json.load() function, create a data.json file.

{
  "data": [
    {
      "color": "red",
      "value": "#f00"
    },
    {
      "color": "green",
      "value": "#0f0"
    },
    {
      "color": "blue",
      "value": "#00f"
    },
    {
      "color": "black",
      "value": "#000"
    }
  ]
}

We will load the data.json file. 

So, in our example, we will load the external json file and convert it into Python types.

Step 2: Open the json file.

To open a file in Python, use the open() method. You can also use the Python with statement to open the file. It returns the file object.

# app.py

import json

# Opening JSON file
fObj = open('data.json',)

Step 3: Load the file object.

To load the file object, use the json.load() method and pass the file object to the function. The json.load() method returns the dictionary.

# It returns JSON object as dictionary
ogdata = json.load(fObj)
print(ogdata)

The data.json file is filled with an array of objects that means when we convert it to the Python data types, we can iterate the dictionary and print the items one by one in the console.

{'data': [{'color': 'red', 'value': '#f00'}, 
          {'color': 'green', 'value': '#0f0'}, 
          {'color': 'blue', 'value': '#00f'}, 
          {'color': 'black', 'value': '#000'}
         ]
}

Step 4: Iterate the dictionary data.

We get the dictionary data from json data, and now we can iterate the values, which is the list of dictionaries. To iterate the list of dictionaries, use the for loop and print the dictionary one by one in the console and then close the file.

# app.py

import json

# Opening JSON file
fObj = open('data.json',)

# It returns JSON object as dictionary
ogdata = json.load(fObj)

# Iterating through the json list
for i in ogdata['data']:
    print(i)

# Closing file
fObj.close()

If you run the above file, then you will see the below output.

{'color': 'red', 'value': '#f00'}
{'color': 'green', 'value': '#0f0'}
{'color': 'blue', 'value': '#00f'}
{'color': 'black', 'value': '#000'}

You can see that we printed the list of dictionaries.

Conclusion

Python json.load() method helps us to read JSON data from text, JSON, or binary file. The load() function returns the data in the form of a Python dictionary.

See also

Python json to dictionary

Python json parse

Python dict to json

Python json to csv

Python list to json

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.