What is JSON?
JSON (JavaScript Object Notation) is a simple format for storing and exchanging data. It is easy to read and write for both humans and machines. Python has a built-in module that allows us to work with JSON data easily called json
.
Using JSON in Python
Importing the JSON Module
To start using JSON in Python, you first need to import the json
module:
x
1
import json # Import the JSON module
Parsing JSON
You can convert a JSON string into a Python dictionary using the json.loads()
function.
Example:
1
10
1
import json # Import the JSON module
2
3
# A JSON string representing a popular Filipino Bread
4
json_string = '{"name": "Pandesal", "type": "bread roll", "ingredients": ["flour", "yeast", "sugar", "salt", "water"]}'
5
6
# Convert JSON string to a Python dictionary
7
bread_data = json.loads(json_string)
8
9
# Access the data
10
print(bread_data["name"]) # Output: Pandesal
Converting Python to JSON
To convert a Python object (like a dictionary) into a JSON string, use the json.dumps()
function.
Example:
1
16
1
import json # Import the JSON module
2
3
# A Python dictionary representing a famous Filipino dessert bread
4
dessert_bread_data = {
5
"name": "Ensaymada",
6
"type": "sweet bread",
7
"toppings": ["butter", "cheese", "sugar"],
8
"is_popular": True
9
}
10
11
# Convert the Python dictionary to a JSON string
12
json_string = json.dumps(dessert_bread_data)
13
14
# Print the JSON string
15
print(json_string)
16
# Output: {"name": "Ensaymada", "type": "sweet bread", "toppings": ["butter", "cheese", "sugar"], "is_popular": true}
Supported Data Types
Dictionary (dict) | JSON Object |
List | JSON Array |
Tuple | JSON Array |
String | JSON String |
Integer | JSON Number |
Float | JSON Number |
Boolean (True/False) | JSON true/false |
None | JSON null |
Example:
1
27
1
import json # Import the JSON module
2
3
# A dictionary with details about various types of Filipino delicacies
4
delicacy_details = {
5
"pandesal": {
6
"flavor": "savory",
7
"texture": "soft",
8
"country_of_origin": "Philippines"
9
},
10
"puto": {
11
"flavor": "sweet",
12
"texture": "fluffy",
13
"country_of_origin": "Philippines"
14
},
15
"bibingka": {
16
"flavor": "sweet and savory",
17
"texture": "moist",
18
"country_of_origin": "Philippines"
19
}
20
}
21
22
# Convert the dictionary to JSON
23
json_data = json.dumps(bread_details)
24
25
# Print the JSON string
26
print(json_data)
27
# Output: {"bibingka": {"flavor": "sweet and savory", "texture": "moist", "country_of_origin": "Philippines"}, "pandesal": {"flavor": "savory", "texture": "soft", "country_of_origin": "Philippines"}, "puto": {"flavor": "sweet", "texture": "fluffy", "country_of_origin": "Philippines"}}
Formatting JSON Output
To format the JSON output for better readability, use indentation.
Example:
1
30
1
import json # Import the JSON module
2
3
# Dictionary representing a delicacy in the Philippines
4
delicacy_data = {
5
"name": "Suman",
6
"type": "rice cake",
7
"ingredients": ["glutinous rice", "coconut milk", "sugar"],
8
"served_with": ["mango", "latik"]
9
}
10
11
# Convert and format the dictionary to a JSON string
12
formatted_json = json.dumps(delicacy_data, indent=4)
13
14
# Print the formatted JSON string
15
print(formatted_json)
16
17
# Output:
18
# {
19
# "ingredients": [
20
# "glutinous rice",
21
# "coconut milk",
22
# "sugar"
23
# ],
24
# "name": "Suman",
25
# "served_with": [
26
# "mango",
27
# "latik"
28
# ],
29
# "type": "rice cake"
30
# }
Sorting JSON Keys
You can sort the keys in your JSON output using the sort_keys
parameter.
Example:
1
25
1
import json # Import JSON module
2
3
# A dictionary representing a type of Filipino bread
4
tasty_breads = {
5
"name": "Pan de Coco",
6
"type": "sweet bread",
7
"filling": "coconut",
8
"texture": "soft",
9
"is_traditional": True
10
}
11
12
# Convert and sort the dictionary to a JSON string
13
sorted_json = json.dumps(tasty_breads, indent=4, sort_keys=True)
14
15
# Print the sorted JSON keys
16
print(sorted_json)
17
18
# Output:
19
# {
20
# "filling": "coconut",
21
# "is_traditional": true,
22
# "name": "Pan de Coco",
23
# "texture": "soft",
24
# "type": "sweet bread"
25
# }