Reading and Writing JSON to a File in Python
Last Updated :
29 May, 2025
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.
Writing JSON to a file in Python
Serializing JSON refers to the transformation of data into a series of bytes (hence serial) to be stored or transmitted across a network. To handle the data flow in a file, the JSON library in Python uses dump() or dumps() function to convert the Python objects into their respective JSON object, so it makes it easy to write data to files. See the following table given below.
PYTHON OBJECT | JSON OBJECT |
---|
Dict | object |
list, tuple | array |
str | string |
int, long, float | numbers |
True | true |
False | false |
None | null |
Method 1: Writing JSON to a file in Python using json.dumps()Â
The JSON package in Python has a function called json.dumps() that helps in converting a dictionary to a JSON object. It takes two parameters:
- dictionary: the name of a dictionary which should be converted to a JSON object.
- indent: defines the number of units for indentation
After converting the dictionary to a JSON object, simply write it to a file using the "write" function.
Python
import json
dictionary = {
"name": "sathiyajith",
"rollno": 56,
"cgpa": 8.6,
"phonenumber": "9976770500"
}
# Serializing json
json_object = json.dumps(dictionary, indent=4)
# Writing to sample.json
with open("sample.json", "w") as outfile:
outfile.write(json_object)
Output:Â
outputMethod 2: Writing JSON to a file in Python using json.dump()Â
Another way of writing JSON to a file is by using json.dump() method The JSON package has the "dump" function which directly writes the dictionary to a file in the form of JSON, without needing to convert it into an actual JSON object. It takes 2 parameters:
- dictionary - the name of a dictionary which should be converted to a JSON object.
- file pointer - pointer of the file opened in write or append mode.
Python
# Python program to write JSON
# to a file
import json
# Data to be written
dictionary = {
"name": "sathiyajith",
"rollno": 56,
"cgpa": 8.6,
"phonenumber": "9976770500"
}
with open("sample.json", "w") as outfile:
json.dump(dictionary, outfile)
Output:Â
outputReading JSON from a file using Python
Deserialization is the opposite of Serialization, i.e. conversion of JSON objects into their respective Python objects. The load() method is used for it. If you have used JSON data from another program or obtained it as a string format of JSON, then it can easily be deserialized with load(), which is usually used to load from a string, otherwise, the root object is in a list or Dict.Â
Reading JSON from a file using  json.load()Â
The JSON package has json.load() function that loads the JSON content from a JSON file into a dictionary. It takes one parameter:
File pointer: A file pointer that points to a JSON file.
Python
import json
with open('sample.json', 'r') as openfile:
json_object = json.load(openfile)
print(json_object)
print(type(json_object))
Output:Â
output
Similar Reads
Reading and Writing lists to a file in Python Reading and writing files is an important functionality in every programming language. Almost every application involves writing and reading operations to and from a file. To enable the reading and writing of files programming languages provide File I/O libraries with inbuilt methods that allow the
5 min read
Reading and Writing to text files in Python Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). Text files: In this type of file, Each line of text is terminated with a special character called EOL
8 min read
Reading and Writing YAML File in Python YAML (YAML Ain't Markup Language) is a human-readable data serialization standard that is commonly used for configuration files and data exchange between languages with different data structures. YAML is preferred in many cases due to its simplicity and readability compared to other formats like JSO
3 min read
Read File As String in Python Python provides several ways to read the contents of a file as a string, allowing developers to handle text data with ease. In this article, we will explore four different approaches to achieve this task. Each approach has its advantages and uses cases, so let's delve into them one by one. Read File
3 min read
Writing to file in Python Writing to a file in Python means saving data generated by your program into a file on your system. This article will cover the how to write to files in Python in detail.Creating a FileCreating a file is the first step before writing data to it. In Python, we can create a file using the following th
4 min read
Append to JSON file using Python 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 Pytho
2 min read
Read JSON file using Python 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 Pytho
4 min read
Read, Write and Parse JSON using Python JSON is a lightweight data format for data interchange that can be easily read and written by humans, and 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. Example of JSON String s = '{"id":0
4 min read
Working With JSON Data in Python 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 tex
6 min read
Write a dictionary to a file in Python A dictionary store data using key-value pairs. Our task is that we need to save a dictionary to a file so that we can use it later, even after the program is closed. However, a dictionary cannot be directly written to a file. It must first be changed into a format that a file can store and read late
3 min read