This Python JSON exercise helps Python developers to practice JSON creation, manipulation, and parsing. As you know, JSON (an acronym for JavaScript Object Notation) is a data-interchange format and is commonly used for client-server communication.
Read Python JSON tutorials for any help.
This exercise includes the following: –
- It has 9 questions and solutions provided for each question.
- Each question includes a specific JSON concept you need to learn. When you complete each question, you get more familiar with JSON encoding and decoding in Python.
Use Online Code Editor to solve exercise questions.
Table of contents
- Exercise 1: Convert the following dictionary into JSON format
- Exercise 2: Access the value of key2 from the following JSON
- Exercise 3: PrettyPrint following JSON data
- Exercise 4: Sort JSON keys in and write them into a file
- Exercise 5: Access the nested key ‘salary’ from the following JSON
- Exercise 6: Convert the following Vehicle Object into JSON
- Exercise 7: Convert the following JSON into Vehicle Object
- Exercise 8: Check whether following json is valid or invalid. If Invalid correct it
- Exercise 9: Parse the following JSON to get all the values of a key ‘name’ within an array
Exercise 1: Convert the following dictionary into JSON format
data = {"key1" : "value1", "key2" : "value2"}
Code language: Python (python)
Expected Output:
data = {"key1" : "value1", "key2" : "value2"}
Show Solution
Exercise 2: Access the value of key2 from the following JSON
import json
sampleJson = """{"key1": "value1", "key2": "value2"}"""
# write code to print the value of key2
Code language: Python (python)
Expected Output:
value2
Show Solution
Exercise 3: PrettyPrint following JSON data
PrettyPrint following JSON data with indent level 2 and key-value separators should be (",", " = ")
.
sampleJson = {"key1": "value1", "key2": "value2"}
Code language: Python (python)
Expected Output:
{ "key1" = "value2", "key2" = "value2", "key3" = "value3" }
Show Solution
Exercise 4: Sort JSON keys in and write them into a file
Sort following JSON data alphabetical order of keys
sampleJson = {"id" : 1, "name" : "value2", "age" : 29}
Code language: Python (python)
Expected Output:
{ "age": 29, "id": 1, "name": "value2" }
Show Solution
Exercise 5: Access the nested key ‘salary’ from the following JSON
import json
sampleJson = """{
"company":{
"employee":{
"name":"emma",
"payble":{
"salary":7000,
"bonus":800
}
}
}
}"""
# write code to print the value of salary
Code language: Python (python)
Expected Output:
7000
Show Solution
Exercise 6: Convert the following Vehicle Object into JSON
import json
class Vehicle:
def __init__(self, name, engine, price):
self.name = name
self.engine = engine
self.price = price
vehicle = Vehicle("Toyota Rav4", "2.5L", 32000)
# Convert it into JSON format
Code language: Python (python)
Expected Output:
{ "name": "Toyota Rav4", "engine": "2.5L", "price": 32000 }
Show Solution
Exercise 7: Convert the following JSON into Vehicle Object
{ "name": "Toyota Rav4", "engine": "2.5L", "price": 32000 }
For example, we should be able to access Vehicle Object using the dot operator like this.
vehicleObj.name, vehicleObj.engine, vehicleObj.price
Show Solution
Exercise 8: Check whether following json is valid or invalid. If Invalid correct it
{ "company":{ "employee":{ "name":"emma", "payble":{ "salary":7000 "bonus":800 } } } }
Show Solution
Solution 1:
Python provides The json.tool
module to validate JSON objects from the command line. Run the following command.
Command: echo "JSON DATA" | python -m json.tool
Output:
Expecting ',' delimiter: line 1 column 68 (char 67)
Just add ','
after "salary":7000
to solve the error.
Solution 2
Exercise 9: Parse the following JSON to get all the values of a key ‘name’ within an array
[ { "id":1, "name":"name1", "color":[ "red", "green" ] }, { "id":2, "name":"name2", "color":[ "pink", "yellow" ] } ]
Expected Output:
["name1", "name2"]