Why this topic matters:JSON (JavaScript Object Notation) is a lightweight data interchange format that's easy for humans to read and write and easy for machines to parse and generate. It's widely used in web development to transmit data between a server and a client or between different parts of an application.
What you'll learn:In this lesson, we will explore how to handle JSON data in Python. You'll learn to load JSON files, manipulate JSON data, and save changes back to a file.
Main explanation with examples:JSON data is a collection of key-value pairs enclosed within curly braces {}
. Each key must be a string, and its corresponding value can be a string, number, array, object, boolean, or null.
Here's an example of JSON data:
{
"name": "John Doe",
"age": 30,
"hobbies": ["reading", "movies", "gaming"],
"isStudent": true
}
In Python, you can work with JSON data using the json
module. To load a JSON file, you use the json.load()
function:
import json
data = json.load(open('data.json', 'r'))
print(data)
To create a new JSON object, you can use the json.dumps()
function:
new_data = {
"name": "Jane Doe",
"age": 28,
"hobbies": ["painting", "music"]
}
with open('new_data.json', 'w') as f:
json.dump(new_data, f)
Real-world code examples:
To load and print the data from a JSON file:
import json
data = json.load(open('data.json', 'r'))
print(data)
To add a new key-value pair to an existing JSON object:
import json
with open('data.json', 'r') as f:
data = json.load(f)
data['pet'] = 'cat'
with open('updated_data.json', 'w') as f:
json.dump(data, f)
To manipulate JSON data:
import json
with open('data.json', 'r') as f:
data = json.load(f)
# Change John Doe's age to 31
data['age'] = 31
# Remove one of John Doe's hobbies
del data['hobbies'][1]
with open('updated_data.json', 'w') as f:
json.dump(data, f)
What causes it:JSON syntax errors are usually caused by incorrect formatting or missing values in the JSON data.
Error message:
Traceback (most recent call last):
File "example.py", line X, in <module>
json.load(open('data.json', 'r'))
File "/usr/lib/python3.7/json/__init__.py", line 293, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value
Solution:Make sure your JSON data is correctly formatted with proper syntax and that all values have corresponding keys.
Why it happens:The JSON data is not well-formed or has missing values, causing the json.load()
function to fail.
How to prevent it:Always ensure that your JSON data adheres to the correct syntax and includes values for all keys.
What causes it:JSON key errors are usually caused by trying to access a non-existent key in a JSON object.
Error message:
Traceback (most recent call last):
File "example.py", line X, in <module>
print(data['non_existent_key'])
KeyError: 'non_existent_key'
Solution:Check if the key exists before trying to access its value.
Why it happens:You are attempting to access a key that does not exist in the JSON object, causing a KeyError
.
How to prevent it:Always check if a key exists before trying to access its value using the in
keyword:
if 'non_existent_key' in data:
print(data['non_existent_key'])
else:
print('Key not found')
pandas
to manipulate JSON data if you're dealing with larger datasets or more complex transformations.json
module.pandas
.