This tutorial aims to help you understand how to effectively handle and manipulate JSON data using Python, a popular and powerful programming language.
By the end of this tutorial, you'll learn:
This tutorial assumes that you have basic knowledge of Python. Familiarity with data types in Python (like dictionaries and lists) would be advantageous but not necessary as we will cover them along the way.
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. Python has a built-in package called json, which can be used to work with JSON data.
Python's json
module has a method called json.loads()
that converts a JSON string into a Python object.
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y["age"])
In the above code, json.loads()
is used to convert the JSON string into a Python dictionary. The printed output will be 30
.
The json
module's json.dumps()
method converts a Python object into a JSON string.
import json
# a Python object (dictionary):
x = {
"name": "John",
"age": 30,
"city": "New York"
}
# convert into JSON:
y = json.dumps(x)
# the result is a JSON string:
print(y)
We can use json.dump()
to write JSON data to a file, and json.load()
to read JSON data from a file.
import json
# Write JSON data to a file
with open('person.json', 'w') as json_file:
json.dump(x, json_file)
# Read JSON data from a file
with open('person.json', 'r') as json_file:
data = json.load(json_file)
print(data)
import json
# a JSON string:
student_json = '{"name":"Emma", "age":22, "city":"London"}'
# parse the JSON string:
student = json.loads(student_json)
# print the "name" value:
print(student["name"]) # Output: Emma
import json
# a Python dictionary:
student = {
"name": "Emma",
"age": 22,
"city": "London"
}
# convert the Python object to a JSON string:
student_json = json.dumps(student)
# print the JSON string:
print(student_json) # Output: {"name": "Emma", "age": 22, "city": "London"}
import json
# a Python dictionary:
student = {
"name": "Emma",
"age": 22,
"city": "London"
}
# write to a JSON file:
with open('student.json', 'w') as json_file:
json.dump(student, json_file)
# read JSON data from the file:
with open('student.json', 'r') as json_file:
data = json.load(json_file)
# print the data:
print(data) # Output: {'name': 'Emma', 'age': 22, 'city': 'London'}
In this tutorial, we learned how to effectively handle JSON data in Python. We learned how to parse JSON data, convert Python data into JSON, and how to read and write JSON data to and from a file. We went through some practical examples and explained each part of the code.
Now, let's put what we learned into practice.
Write a Python script to convert the following Python dictionary to a JSON string and print it.
person = {
"name": "John",
"age": 30,
"city": "New York"
}
Write a Python script to parse the following JSON string into a Python object and print the city
value.
person_json = '{"name":"John", "age":30, "city":"New York"}'
Write a Python script to write a Python list to a JSON file and then read it back.
numbers = [1, 2, 3, 4, 5]
For further practice, you can try to work with more complex JSON data and try to write and read it from a file.