A cheat sheet for working with JSON Data in Python

A cheat sheet for working with JSON Data in Python

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 = json.load(file)

pprint.pprint(data) # 1st Way

We can use the pretty print library.

The second way is using the JSON library the print function

import json 
import pprint

path_to_file = "data.json"
with open(path_to_file) as file:
    data = json.load(file)

print(json.dumps(data, indent=4, sort_keys=True)) # 2nd Way

Loading JSON from a string

We will use json.loads()

import json
import pprint

stringJSON = '''
[
    {
        "color": "red",
        "value": "#f00"
    },
    {
        "color": "green",
        "value": "#0f0"
    }
]
'''

data = json.loads(stringJSON)
pprint.pprint(data)

Converting Objects to JSON

We will use json.dumps(). Below are some commonly formated data you can convert to a JSON object.

Dictionary

import json

data = {
    "key1" : "value1",
    "key2" : "value2",
    "key3" : "value3"
}

jsonData = json.dumps(data, indent=4)

print(jsonData)

List of Dictionaries

import json

data = [
    {
        "dictionary1" : "value1"
    },
    {
        "dictionary2" : "value2"
    },
    {
        "dictionary3" : "value3"
    }
]

jsonData = json.dumps(data, indent=4)

print(jsonData)

Dictionary of Dictionaries

import json

data = {
    "dictionary1" : {"key1" : "value1"},
    "dictionary2" : {"key2" : "value2"},
    "dictionary3" : {"key3" : "value3"}
}

jsonData = json.dumps(data, indent=4)

print(jsonData)

List of Lists

import json

data = [
    [1,2,3,4],
    ["helo" , "world" , "python"]
]

jsonData = json.dumps(data, indent=4)

print(jsonData)

Saving JSON data into a file

We will use the json.dump() function. If the file doesn't exist, it will create a new file. If the file does exist, it will overwrite the data in the file

import json

data = [
    {
        "dictionary1" : "value1"
    },
    {
        "dictionary2" : "value2"
    },
    {
        "dictionary3" : "value3"
    }
]

nameOfFile = "jsonOutput.json"

with open(nameOfFile,"w") as file:
    json.dump(data, file)
    print("Saved File")

Parsing JSON

Parsing a JSON file depends on the format of the data, it could be a simple dictionary, list of dictionaries etc. The logic to parse JSON data will vary case by case. The syntax is the one we follow while traversing lists or dictionaries. The following code snippets might be helpful. In most cases, you will have to use some combination of the below cases.

Parsing JSON stored as a dictionary

This is the "Happy Case"

import json

'''
DATA FORMAT
{   "key1" : "value1", 
    "key2" : "value2",
    "key3" : "value3"
}
'''

with open("data.json") as file:
    jsonData = json.load(file)

for key,value in jsonData.items():
    print(key,value)

Parsing JSON stored as a list of dictionaries

import json

'''
DATA FORMAT
[
    { "dictionary1" : "value1"},
    { "dictionary2" : "value2"},
    { "dictionary3" : "value3"}
]
'''

with open("data.json") as file:
    jsonData = json.load(file)

for item in jsonData:
    for key,value in item.items():
        print(key,value)

Parsing JSON stored as a dictionary of dictionaries

import json

'''
DATA FORMAT
{
    "dictionary1" : {"key1" : "value1"},
    "dictionary2" : {"key2" : "value2"},
    "dictionary3" : {"key3" : "value3"}
}
'''

with open("data.json") as file:
    jsonData = json.load(file)

for jsonName,jsonObject in jsonData.items():
    print(jsonName)
    for key,value in jsonObject.items():
        print(key,value)

Parsing JSON stored as a list of lists

import json

'''
DATA FORMAT
[
    [1,2,3,4],
    ["helo" , "world" , "python"]
]
'''

with open("data.json") as file:
    jsonData = json.load(file)

for listItem in jsonData:
    for element in listItem:
        print(element)

JSON Data Transformation

In the below sections we will transform some JSON Data and store it in a new file

Case1: List of Dictionaries to a Dictionary

UntitledCASE1.png

import json

with open("data.json") as file:
    jsonData = json.load(file)

result = {}

for item in jsonData:
    result[item['color']] = item['value']

with open("jsonOutput.json","w") as file:
    json.dump(result, file)
    print("Saved File")

Case2: Dictionary of Dictionaries to a List of Dictionaries

UntitledCASE2.png

import json

with open("data.json") as file:
    jsonData = json.load(file)

result = []

for jsonName,jsonObject in jsonData.items():
    result.append(jsonObject)


with open("jsonOutput.json","w") as file:
    json.dump(result, file)
    print("Saved File")

Case3: List of Dictionaries to a List of Lists

UntitledCASE3.png

import json

with open("data.json") as file:
    jsonData = json.load(file)

colors = []
colorValues = []

for item in jsonData:
    colors.append(item['color'])
    colorValues.append(item['value'])

result =[ colors , colorValues]

with open("jsonOutput.json","w") as file:
    json.dump(result, file)
    print("Saved File")

Connect with me on LinkedIn, Twitter