How to Parse JSON Data in Python (Read and Write)

JSON is an object notation format that is used to store and send data. It is a popular API response format and can be decoded by most mainstream programming languages, such as Python, JavaScript and PHP.

 

In this tutorial, we will learn how to parse JSON data in Python use the data and convert it back to JSON markup. 

 

Import the JSON package

Python has a native JSON package that makes parsing JSON data easier. To use it you will have to import it into your Python program. Put the following at the top of your program:

 

import json

 

Parse JSON Data

Now the json package is loaded we can parse JSON data using the .loads() method. 

 

fruit_json = '{"fruit": "Apple","size": "Large","color": "Red"}'

fruit_dict = json.loads(fruit_json)

print(fruit_dict)
print(type(fruit_dict))
{'fruit': 'Apple', 'size': 'Large', 'color': 'Red'}
<class 'dict'>

 

Using the type() we can see that json.loads() converts the JSON data into a Python Dictionary.

 

Open Local File and Parse the JSON Data

To parse JSON from a local file we will first need to open it using the Python open() method before passing it into the json.load() method.

 

{
    "fruit": "Apple",
    "size": "Large",
    "color": "Red"
}
json_path = 'example.json'

with open(json_path) as f:
    fruit_dict = json.load(f)

print(fruit_dict)
{'fruit': 'Apple', 'size': 'Large', 'color': 'Red'}

 

Open Remote File Over HTTPS and Parse the JSON Data

To open a file from a remote address on the web over HTTP/HTTPS import the Python requests package.

 

import requests

 

Now we can use the requests.get() method on the remote JSON file to get the data and then parse it using the json.loads() method.

 

In the example below we are importing the JSON from this file:

https://www.skillsugar.com/examples/json/example_1.json

 

json_path = 'https://www.skillsugar.com/examples/json/example_1.json'

resp = requests.get(json_path)

print(json.loads(resp.text))
{'fruit': 'Apple', 'size': 'Large', 'color': 'Red'}

 

Convert Python Dictionary to a JSON String

To convert a Python dictionary back into a string in preparation for uploading to a server .etc, use the json.dumps() method.

 

json_data = json.dumps(fruit_dict)
    
print(json_data)
print(type(json_data))
{"fruit": "Apple", "size": "Large", "color": "Red"}
<class 'str'>

 

Python dictionary to JSON equivalent conversion table:

 

PythonJSON
strstring
int, floatnumber
Truetrue
Falsefalse
Nonenull
dictobject
list, tuplearray

 

Write JSON to a File

To write JSON data to a file, open a file in write (w) mode using the Python open() method. Then use the json.dump() method to convert the Python dictionary to a string before that data is saved to the file.

 

with open('fruit.json', 'w') as f:
    json.dump(fruit_dict, f)

 

note - If a file does not exist one will be created.

 

PrettyPrint the JSON data to a File

You will notice that the JSON data that was written to a file in the previous example was minified and not human friendly. To make the code pretty we can add two additional arguments to the json.dump() method; indent and sort_keys.

 

with open('fruit.json', 'w') as f:
    json.dump(fruit_dict, f, indent = 4, sort_keys=True)
{
    "color": "Red",
    "fruit": "Apple",
    "size": "Large"
}

 

indent sets the number of spaces to indent the code by and sort_keys sorts properties by their key name.

 

Conclusion

You now know how to use JSON data in Python by parsing it into a dictionary using the json package.  

json parse string dictionary data