The Wayback Machine - https://web.archive.org/web/20240814083010/https://www.geeksforgeeks.org/json-load-in-python/
Open In App

json.load() in Python

Last Updated : 02 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 Python

json.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.

pyhton-append-json1

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:

python-read-json-output1

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.



Similar Reads

Python - Difference Between json.load() and json.loads()
JSON (JavaScript Object Notation) is a script (executable) file which is made of text in a programming language, is used to store and transfer the data. It is a language-independent format and is very easy to understand since it is self-describing in nature. Python has a built-in package called json. In this article, we are going to see Json.load a
3 min read
Load JSON String into Pandas DataFrame
Let us see how can we use a dataset in JSON format in our Pandas DataFrame. This can be done using the built-in read_json() function. It enables us to read the JSON in a Pandas DataFrame. Example : Consider the JSON file path_to_json.json : # importing the module import pandas # reading the file data = df.read_json("path_to_json.json") #
1 min read
Python - Difference between json.dump() and json.dumps()
JSON is a lightweight data format for data interchange which can be easily read and written by humans, easily parsed and generated by machines. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called json. Note: For more information, refer to Working With JSON Data in Python json.dumps() json.
2 min read
numpy.load() in Python
numpy.load() function return the input array from a disk file with npy extension(.npy). Syntax : numpy.load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding='ASCII') Parameters: file : : file-like object, string, or pathlib.Path.The file to read. File-like objects must support the seek() and read() methods. mmap_mode : If not Non
2 min read
How to Dynamically Load Modules or Classes in Python
Python provides a feature to create and store classes and methods and store them for further use. The file containing these sets of methods and classes is called a module. A module can have other modules inside it. Note: For more information, refer to Python Modules Example: A simple example of importing a module is shown below in which, there are
3 min read
How to use Pickle to save and load Variables in Python?
Serialization is a technique used to save the state of an object from any process. We can later use this state by deserialization, to continue the process. Pickle is a python module that makes it easy to serialize or save variables and load them when needed. Unlike JSON serialization, Pickle converts the object into a binary string. JSON is text sp
2 min read
How to Fix 'NoSuchModuleError: Can't Load Plugin: sqlalchemy.dialects.postgres.psycopg2' in Python
SQLAlchemy is a powerful SQL toolkit and Object-Relational Mapping (ORM) library for Python. It facilitates interaction with various databases using Python code. However, while working with SQLAlchemy, developers may encounter the error: NoSuchModuleError: Can't Load Plugin: sqlalchemy.dialects.postgres.psycopg2. This error indicates a problem with
3 min read
Load testing using LOCUST
Locust is an open-source load-testing tool. Load testing is a type of software testing that is conducted to check the tolerance/behavior of the system under a specific expected load. The target of locust is load-testing websites and checking the number of concurrent users a system can handle. During a locust test, a swarm of locusts will attack the
2 min read
Save/load game Function in Pygame
Pygame is a free-to-use and open-source set of Python Modules. And as the name suggests, it can be used to build games, and to save game data or the last position of the player you need to store the position of the player in a file so when a user resumes the game its game resumes when he left. In this case, the text file with JSON format stores the
4 min read
How to Load a Massive File as small chunks in Pandas?
Pandas in flexible and easy to use open-source data analysis tool build on top of python which makes importing and visualizing data of different formats like .csv, .tsv, .txt and even .db files. For the below examples we will be considering only .csv file but the process is similar for other file types. The method used to read CSV files is read_csv
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg