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.
  · 4 min read · Updated jul 2020 · Python Standard Library


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 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))

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 exist 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)

json.load() function will automatically return a Python dictionary, which ease 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.

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, or when you scrape data from the web.

I've included a simple example in full code page, where we make an API request and save the resulting data to a JSON file.

Want to Learn More ?

Finally, many of the Python concepts aren't discussed in detail here, if you feel you want to dig more to 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