json.load() in Python
Last Updated :
02 Aug, 2024
The full-form of
JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called
json
. To use this feature, we import the json package in Python script. The text in JSON is done through quoted-string which contains the value in key-value mapping within
{ }
. It is similar to the dictionary in Python.
Note:
For more information, refer to
Working With JSON Data in Pythonjson.load()
json.load()
takes a file object and returns the json object. A
JSON object
contains data in the form of key/value pair. The keys are strings and the values are the JSON types. Keys and values are separated by a colon. Each entry (key/value pair) is separated by a comma.
Syntax :
json.load(file_object)
Argument :
It takes file object as a parameter.
Return :
It return json object.
Example:
Let’s suppose the JSON looks like this.

We want to read the content of this file. Below is the implementation.
Python
# Python program to read
# json file
import json
# Opening JSON file
f = open('data.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in data['emp_details']:
print(i)
# Closing file
f.close()
Output:

Here, we have used the
open()
function to read the JSON file. Then, the file is parsed using
json.load()
method which gives us a dictionary named data.
json.load() in Python – FAQs
What does JSON load() do in Python?
The json.load() function in Python is used to parse a JSON file into a Python object (such as a dictionary). It reads data from a file-like object, interprets the JSON data, and converts it into Python data structures.
How to Load Data from JSON in Python?
To load data from a JSON file into Python, you use the json.load() function, which takes a file object and returns a Python dictionary.
Example:
import json
# Open a JSON file and load its contents into a Python dictionary
with open('data.json', 'r') as file:
data = json.load(file)
print(data)
This code reads from a JSON file named data.json and prints its contents as a dictionary.
How to Load JSON from String in Python?
If you have JSON data in a string format, you can use the json.loads() function (the “s” stands for “string”) to parse the JSON string into a Python dictionary.
Example:
import json
# JSON string
json_string = '{"name": "John", "age": 30, "city": "New York"}'
# Convert JSON string to Python dictionary
data = json.loads(json_string)
print(data)
This code takes a JSON formatted string and converts it into a Python dictionary.
How to Load Object to JSON in Python?
To convert a Python object (like a dictionary) to a JSON formatted string, use the json.dumps() method. This might have been a misunderstanding in your question, as usually, we talk about “loading” data into Python and “dumping” data from Python to JSON.
Example:
import json
# Python dictionary
person = {"name": "John", "age": 30, "city": "New York"}
# Convert dictionary to JSON formatted string
json_string = json.dumps(person)
print(json_string)
This converts a Python dictionary to a JSON formatted string.
How to Import a JSON File?
To import data from a JSON file into a Python script, use the json.load() function as previously demonstrated. This is the standard way to “import” JSON data into Python objects for processing.
Example:
import json
# Importing a JSON file
with open('data.json', 'r') as file:
data = json.load(file)
print(data)
This snippet will read the JSON data from data.json and convert it into a Python dictionary, effectively “importing” it.
These operations showcase the versatility of the JSON module in Python for handling JSON data, whether coming from strings, files, or needing conversion between JSON and Python formats.