Exercises on JSON

#Exercise 1: Convert the following dictionary into JSON format

data = {“key1” : “value1”, “key2” : “value2”}

o/p = data = {“key1” : “value1”, “key2” : “value2”}

#Exercise 2: Access the value of key2 from the following JSON…

sampleJson = “”“{“key1”: “value1”, “key2”: “value2”}”“”
write code to print the value of key2

o/p = value2

#Exercise 3: Sort JSON keys in and write them into a file?

sampleJson = {“id” : 1, “name” : “value2”, “age” : 29}

o/p

{
“age”: 29,
“id”: 1,
“name”: “value2”
}

#Excercise 4 : Access the nested key ‘salary’ from the following JSON?

sampleJson = “”“{
“company”:{
“employee”:{
“name”:“emma”,
“payble”:{
“salary”:7000,
“bonus”:800
}
}
}
}”“”

o/p 7000

#Exercise 5: 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”
]
}
]
o/p : [“name1”, “name2”]