How to Work with JSON Files in Python

Learn how to save (serialize) and load (deserialize) JSON files in Python using the built-in json module.
  · 5 min read · Updated sep 2021 · Python Standard Library


Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.

JSON (JavaScript Object Notation) is a lightweight open standard data-interchange file format, that uses human-readable text for transmitting data.

Although you may conclude from the name that it's a Javascript data format. Well, not exactly, JSON is a text format that is completely language-independent and uses conventions that are familiar of most popular programming languages such as Python.

In this tutorial, you will use Python for:

Luckily for us, Python has a built-in module json, that is sufficient for our work, let's get started!

Saving JSON Data

Python dictionaries are very similar to JSON format, in fact, you can save a dictionary in very few lines of code:

import json

# example dictionary to save as JSON
data = {
    "first_name": "John",
    "last_name": "Doe",
    "email": "[email protected]",
    "salary": 1499.9, # just to demonstrate we can use floats as well
    "age": 17,
    "is_real": False, # also booleans!
    "titles": ["The Unknown", "Anonymous"] # also lists!
}

# save JSON file
# 1st option
with open("data1.json", "w") as f:
    json.dump(data, f)

Once you execute the above code, you'll notice data1.json file appeared in your working directory. We've opened the file in write mode, and used the json.dump() function to serialize the Python dictionary as a JSON formatted stream to the opened file.

The resulting file will look something like this:

{
    "first_name": "John",
    "last_name": "Doe",
    "email": "[email protected]",
    "salary": 1499.9,
    "age": 17,
    "is_real": false,
    "titles": [
        "The Unknown",
        "Anonymous"
    ]
}

That's one way of saving as JSON, you can use json.dumps() function as well:

# 2nd option
with open("data2.json", "w") as f:
    f.write(json.dumps(data, indent=4))

The json.dumps() function returns the dictionary as a JSON parsed string, you may want this string for other use, we just saved it to a file just to make you aware that it does exist.

Notice I added indent=4 this time as a parameter to json.dumps() function, this will pretty-print JSON array elements and object members, if you use indent=0, it'll only print new lines, and if it's None (default), then it's dumped in a single line (not human-readable). The indent keyword exists both in dump() and dumps() functions.

Handling Non-ASCII Characters

If your data contains non-ASCII characters, and you don't want Unicode instances on your JSON file (such as \u0623), then you should pass ensure_ascii=False to json.dump() function:

unicode_data = {
    "first_name": "أحمد",
    "last_name": "علي"
}

with open("data_unicode.json", "w", encoding="utf-8") as f:
    json.dump(unicode_data, f, ensure_ascii=False)

Resulting file:

{"first_name": "أحمد", "last_name": "علي"}

Loading JSON Data

It is pretty straightforward to deserialize JSON files and load them into Python, the below code loads the previously saved JSON file:

# read a JSON file
# 1st option
file_name = "data1.json"
with open(file_name) as f:
    data = json.load(f)
    
print(data)

The json.load() function will automatically return a Python dictionary, which eases our work with JSON files, here is the output:

{'first_name': 'John', 'last_name': 'Doe', 'email': '[email protected]', 'salary': 1499.9, 'age': 17, 'is_real': False, 'titles': ['The Unknown', 'Anonymous']}

Similarly, you can also use json.loads() function to read a string instead:

# 2nd option
file_name = "data2.json"
with open(file_name) as f:
    data = json.loads(f.read())

print(data)

So we've read the file content first using read() method, and then we pass it to json.loads() function to parse it.

Simple Real World Toy Example

In this section, we'll be using an API request to a remote web server, and save the resulting data to a JSON file, here's the complete code for that:

import requests
import json


# make API request and parse JSON automatically
data = requests.get("https://jsonplaceholder.typicode.com/users").json()
# save all data in a single JSON file
file_name = "user_data.json"
with open(file_name, "w") as f:
    json.dump(data, f, indent=4)
    print(file_name, "saved successfully!")

# or you can save each entry into a file
for user in data:
    # iterate over `data` list
    file_name = f"user_{user['id']}.json"
    with open(file_name, "w") as f:
        json.dump(user, f, indent=4)
        print(file_name, "saved successfully!")


# load 2nd user for instance
file_name = "user_2.json"
with open(file_name) as f:
    user_data = json.load(f)
    
print(user_data)
print("Username:", user_data["username"])

Note: To run the above code, you need to install the requests library via: pip install requests

Conclusion

Now you know how to use dump(), dumps(), load() and loads() functions in the json module and you have the ability to work with JSON data in Python.

As a developer, you'll likely have to interact with it frequently, you'll encounter JSON a lot of times, especially when working with REST APIs as we have shown in the example, or when you scraping data from the web.

Every code in this tutorial is included in the full code page, check it out!

Want to Learn More?

Finally, many of the Python concepts aren't discussed in detail here, if you feel you want to dig more into Python, I highly suggest you get one of these amazing courses:

Check Python's json module official documentation for further details.

Learn also: How to Use Regular Expressions in Python.

Happy Coding ♥

View Full Code
Sharing is caring!



Read Also




Comment panel