Open In App

Reading and Writing JSON to a File in Python

Last Updated : 28 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Reading JSON from a file using Python

Reading JSON in Python means retrieving JSON data from a file or string and converting it into Python objects like dictionaries or lists. This process is called deserialization. Python offers two main ways to read JSON data:

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

Example: Read a JSON file and convert its contents to a Python dictionary.

Python
import json

# Read from file and parse JSON
with open("sample.json", "r") as f:
    data = json.load(f)

print(data)
print(type(data))

Output: 

Output_usingjsonload
Output using json.load()

Explanation: We open sample.json in read mode and use json.load() to parse its content into a Python dictionary, which we then print along with its data type.

2. Using json.loads()

json.loads() function is used to parse a JSON string and convert it into a Python object. It takes one parameter:

JSON string: A valid JSON-formatted string.

Example: Convert a JSON-formatted string into a Python dictionary.

Python
import json
json_str = '{"name": "Francis", "age": 25, "city": "New York"}'
data = json.loads(json_str)

print(data)
print(type(data))
Output_usingjsonloads
Output using json.loads()

Explanation: We define a JSON string and use json.loads() to parse it into a Python dictionary, then print the resulting object and its type.

Writing JSON to a file in Python

Writing data to a JSON file in Python involves converting Python objects like dictionaries into JSON format and saving them to a file. This process is called serialization. Let's explore two methods to write a JSON file in Python.

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

Example: Convert a dictionary to a JSON string and write it to a file.

Python
import json
data = {
    "name": "sathiyajith",
    "rollno": 56,
    "cgpa": 8.6,
    "phone": "9976770500"
}

json_str = json.dumps(data, indent=4)
with open("sample.json", "w") as f:
    f.write(json_str)

Output: 

Output_usingjsondumps
Output using json.dumps()

Explanation: We create a dictionary, convert it to a JSON-formatted string using json.dumps() with 4-space indentation and write it to sample.json using the write() method.

2. Using json.dump() 

Another way of writing JSON to a file is by using json.dump() method. The JSON package has "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.

Example: Directly write a dictionary to a JSON file without converting it first.

Python
import json
data = {
    "name": "sathiyajith",
    "rollno": 56,
    "cgpa": 8.6,
    "phone": "9976770500"
}

with open("sample.json", "w") as f:
    json.dump(data, f)

Output

Output_usingjsondump
Output using json.dump()

Explanation: We create a dictionary and use json.dump() to directly write it to sample.json, skipping the need to manually convert it to a JSON string.

Python vs JSON Object Mapping

PYTHON OBJECTJSON OBJECT
Dictobject
list, tuplearray
strstring
int, long, floatnumbers
Truetrue
Falsefalse
Nonenull

Reading and Writing JSON to a File in Python

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy