Using JSON in Python
What is JSON?
The JSON abbreviation stands for JavaScript Object Notation. By definition, it represents a readable, lightweight format used for exchanging and storing data between different applications or systems. As a subset of JavaScript, its roots trace back to the JavaScript object literal syntax.
However, the usage isn't limited to JavaScript. In fact, JSON is a widely used data format that is supported by many programming languages, including Python.
Python has built-in support for JSON through the json
package. The package provides a neat way to encode and decode JSON data.
How to Read a JSON file in Python
Let's say we have a separate JSON formatted file with elaborate data and we need to read it and convert it into a Python object. The approach is quite straightforward.
import json
# Open the file
with open("example.json", "r") as file:
# Load the JSON data from the file
data = json.load(file)
# Print the data
print(data)
We first import the json
module. Then, we use the open
function to access the file example.json
in read mode. Next, we use the json.load
method to load the JSON data from the file into a Python object.
Converting JSON in Python
Deserializing JSON data means converting it from its string representation to a Python object. Same as before, we can rely on the built-in module to deserialize the data.
If we have a JSON string, the json.loads()
method takes the string as input and returns a Python object, or more precisely, a dictionary.
import json
# JSON string
json_string = '{"employee": "Bob", "age": 28, "job": "designer"}'
# Deserialize the JSON string
data = json.loads(json_string)
# Print the data
print(data["age"]) #Output 28
Transforming Python Objects to JSON
In contrast, serializing Python objects means converting them to their JSON representation. The module provides two methods for serializing Python objects such as json.dump()
and json.dumps()
.
To demonstrate, we'll look at an example with the json.dumps()
method taking a Python dictionary as input and returning a JSON string.
import json
# Python object
dict_example = {
"name": "Guido van Rossum",
"age": 67,
"developer": True
}
# Serialize the Python object
json_string = json.dumps(dict_example)
# Print the JSON string
print(json_string)
# Output: {"name": "Guido van Rossum", "age": 67, "developer": true}
Sometimes, the JSON string produced may not be formatted in a way that is easy to read. To solve this, we can use the indent
argument to specify the number of spaces to use for indentation.
import json
# Python object
dict_example = {
"name": "Guido van Rossum",
"age": 67,
"developer": True
}
# Serialize the Python object with indentation
json_string = json.dumps(dict_example, indent=4)
# Print the JSON string
print(json_string)
# Output:
# {
# "name": "Guido van Rossum",
# "age": 67,
# "developer": true
# }
For clarity, here's a table showing how Python Objects are converted to JSON representation:
Python | JSON |
---|---|
dict | Object |
list, tuple | Array |
str | String |
int, float | Number |
True | true |
False | false |
None | null |
Caveats and Limitations
When working with JSON data in Python, there are a few things to keep in mind:
JSON data can only represent a limited set of data types, including objects, arrays, strings, numbers, booleans, and null.
JSON keys must be strings, while the values can be of any type.
JSON data is case-sensitive.
JSON data must be encoded in UTF-8.