Reading and Writing JSON to a file

JSON is the abbreviation of 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. JSON (JavaScript Object Notation) is a popular data format used for representing structured data. It’s common to transmit and receive data between a server and web application in JSON format. In Python, JSON exists as a string. It’s also common to store a JSON object in a file. For example:

p = '{"name": "Bob", "languages": ["Python", "Java"]}'

Writing JSON to a file in Python using json.dumps()

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.

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.

import json

# Data to be written
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)

Writing JSON to a file in Python using json.dump()
There is 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 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)

Difference between json.dumps() and json.dump()

json.dumps() method can convert a Python object into a JSON string Where as json.dump() method can be used for writing to JSON file.

Reading JSON from a file using Python
During reading from a file JSON object needs to be converted back to their respective Python objects. The load() method is used for it. 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.

import json
 
# Opening JSON file
with open('sample.json', 'r') as openfile:
 
    # Reading from json file
    json_object = json.load(openfile)
 
print(json_object)
print(type(json_object))
Some of the material and examples are taken from the following sources. 
1) https://www.geeksforgeeks.org/reading-and-writing-json-to-a-file-in-python/
2) https://www.programiz.com/python-programming/json
3) https://www.w3schools.com/python/python_json.asp

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *