Python json load: How to Load JSON File in Python
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.
Python has a built-in package json, and to use json, import the json package in the script.
Python json load
Python json.load() is a built-in method that accepts a file object and returns the json object. Using the json.load() method, you can turn JSON encoded/converted data into Python data types; this process is known as JSON decoding.
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.
How to load json file in Python
To load a json file in Python, use the json.load() function. Open a json file using the open() method and create a file object and pass that file object to 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 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 which 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 files. The load() function returns the data in the form of a dictionary.