Sitemap
Programming Tips

Content related to programming: tips, code-snippets, concepts.

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 pprint
path_to_file = "data.json"
with open(path_to_file) as file:
data =

--

--

Programming Tips

Published in Programming Tips

Content related to programming: tips, code-snippets, concepts.

Rahul Banerjee

Written by Rahul Banerjee

Software Developer | Views are my own

No responses yet