How to Read and Write to a JSON File in Python

JavaScript Object Notation (JSON) is a powerful programming tool for exchanging data rapidly across various programming platforms. Whether you’re storing data or making an API, converting your data into JSON makes it reusable and callable, regardless of the technology accessing it.

To foster effective communication between Python and other programming languages, including JavaScript, you can provide your data as a JSON object.

Here’s how to read and write to a JSON file in Python.

How to Write Directly to a JSON File

There’s a thin line between a JSON object and a Python dictionary.

So it’s easy to store a Python dictionary as JSON. But to make it work, you need the json parser library.

To get started, create a JSON file in your project root directory.

Create and open a Python file to the same directory. You can then write a dictionary into the JSON file using Python:

import json
data = {"MUO":"Media", "Google":"Search", "Python":"Language"}
with open("myfile.json", "w") as j:
json.dump(data, j)

You can also write a more complex array into your file:

import json
data = {"Sites":[{"MUO":"Media", "Google":"Search", "Python":"Language"}]}
with open("myfile.json", "w") as j:
json.dump(data, j)

How to Store a List as JSON in Python

You might have a list or two, and you want to save them as JSON. A good practice is to convert them into a dictionary before writing them to a JSON file.

There are many ways to convert a list into a Python dictionary.

The example code below converts the list into a dictionary before writing it to a JSON object:

import json
data = ["MUO", "Media", "Google", "Search", "Python", "Language"]
data = {data[i]:data[i+1] for i in range(0, len(data), 2)} #convert data into a dictionary
with open("myfile.json", "w") as j:
json.dump(data, j)

Related: How to Use For Loops in Python

And if you want to merge two lists into one before writing them into a JSON file:

import json
data = ["MUO", "Google", "Python"]
data2 = ["Media", "Search", "Language"]
outputData = {data[i]:data2[i] for i in range(len(data))} #merge the two lists into a dictionary
with open("myfile.json", "w") as j:
json.dump(outputData, j)

Accessing Your JSON Data

It’s easy to access and query your data from a JSON file using Python:

import json
with open("test.json", "r") as j:
mydata = json.load(j)
print(mydata)
Output: {'MUO': 'Media', 'Google': 'Search', 'Python': 'Language'}

Learn More: How to Read JSON With Python

And if you want to get specific data from your JSON file:

with open("test.json", "r") as j:
mydata = json.load(j)
print(mydata["MUO"])
Output: Media

Query Faster in Python With JSON

In addition to being cross-platform, JSON objects are light and can improve the response speed during queries. And as you’ve seen, making outputs available as JSON objects in Python is simple.

There may be some differences between the examples here and actual implementation in a real-life project, though. This is the basic knowledge you need to get started. Thankfully, you can even use a NoSQL database like CouchDB with Python to store inputs directly as JSON.

Source: makeuseof.com

Related posts

Connections #329: Today’s Answer and Clues (Sunday, May 5, 2024)

PC Fans Running at Full Throttle? 6 Fixes

The 5 Best Free Driver Updaters for Windows