A cheat sheet for working with JSON Data in Python
Tags: python, 100daysofcode, programming, computerscience
Published: 2021-06-05 22:07:51

In this article, we will be doing some common operations while working with JSON data in Python

{% twitter 1400297064492322821 %}


Let's assume we have a JSON file with the following data

```JSON
[
	{
		"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
```python
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
```python
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 
```python
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() 
```python
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
```python
import json

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

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

print(jsonData)
```

#### List of Dictionaries
```python
import json

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

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

print(jsonData)
```

#### Dictionary of Dictionaries
```python
import json

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

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

print(jsonData)
```

#### List of Lists
```python
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

```python
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" 
```python
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
```python
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
```python
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
```python
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](https://cdn.hashnode.com/res/hashnode/image/upload/v1622925689137/5eFY_QMIA.png)

```python
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](https://cdn.hashnode.com/res/hashnode/image/upload/v1622927518228/urJ51QxIb.png)

```python
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](https://cdn.hashnode.com/res/hashnode/image/upload/v1622928048461/rp7J1UqSe.png)

```python
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")
```

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>Connect with me on <a href="https://www.linkedin.com/in/rahulbanerjee2699/" rel="noreferrer noopener" target="_blank">LinkedIn</a>, <a href="https://twitter.com/rahulbanerjee99" rel="noreferrer noopener" target="_blank">Twitter</a></p></blockquote>
<!-- /wp:quote -->