Member-only story
A cheat sheet for working with JSON Data in Python — Python and Data Science for Everyone
4 min readJun 5, 2021
Press enter or click to view image in full size
In this article, we will be doing some common operations while working with JSON data in Python
Let’s assume we have a JSON file with the following data
[
{
"color": "red",
"value": "#f00"
},
{
"color": "green",
"value": "#0f0"
},
{ ... },
{ ... },
]I have truncated the data but it’s basically a list of objects with a color and its corresponding hex value.
Reading JSON Files
import json path_to_file = "data.json"
with open(path_to_file) as file:
data = json.load(file)print(data)
Pretty printing JSON
The output is hard to read, let’s improve its formatting. There are a couple of ways we can do that
import json
import pprintpath_to_file = "data.json"
with open(path_to_file) as file:
data =…