Introduction to JSON
In the previous article, we have seen together how we can store data in a YAML file. Now let’s talk about JSON.
YAML vs JSON
JSON or JavaScript Object Notation is an open standard file format, and data interchange format, that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value).
It is a very common data format, with a diverse range of applications, such as serving as a replacement for XML in AJAX systems.
So what is the difference with YAML ?
You can represent the same data in almost the same way in both format.
YAML example :
car : color: blue model: audi A3 owners: - John Doe - Jane Doe
JSON example :
{ "car": { "color": "blue", "model": "audi A3", "owners": [ "John Doe", "Jane Doe" ] }
}
As you can see the main syntax differences are the following :
- To organize data into lists and dictionaries : YAML uses indentation / JSON uses curly brakets.
- For items in list : YAML uses a dash / JSON uses square brakets.
You can find an online converters if you want to convert YAML to JSON :Click Here or Here
How to retrieve data through JSON Path – Examples
JSON Path is a query language that can help you parse data represented in a YAML or JSON format.
For example if you have the following data :
{ "car": { "color": "blue", "model": "audi A3" }, "truck": { "color": "white", "model": "Renault Magnum" } }
And you want to retrieve all information only about the car, you will use the JSON query $.car to get the following result :
{ "color": "blue", "model": "audi A3" }
But if you want to have the car color, you will use the following JSON query : $.car.color :
{ "color": "blue", }
See, that’s pretty easy, you just follow the path to the value you are interested in.
Before practicing together, one more example which will use arrays.
We have the following source :
{ "car": { "color": "blue", "model": "audi A3", "owners": [ "John Doe", "Jane Doe" ] }
}
And we want to retrieve the first owner of the car.
The correct JSON Query will be : $.car.owners[0] as we are looking for a value in an array.
Now let’s practice !
Don’t scroll to fast if you don’t want to see the answers 😉
Exercice 1
How would you extract value of the key “name” ?
{ "name": "Doe", "age": "33" }
JSON Query : $.name
Exercice 2
How would you extract all values and properties from this source data ?
{ "car": "audi", "owner": "John Doe" }
JSON Query : $.*
Exercice 3
How would you extract all colors for the car and the bus ?
{ "car": { "color": "blue", "price": "$20,000" }, "bus": { "color": "white", "price": "$120,000" } }
JSON Query : $.*.color – As you can see, the * will take all entries
Exercice 4
How would you extract all colors for the car and the bus ?
{ "vehicles": { "car": { "color": "blue", "price": "$20,000" }, "bus": { "color": "white", "price": "$120,000" } } }
JSON Query : $.*.color
Exercice 5
How would you list the model of all wheels from the car ?
{ "car": { "color": "blue", "price": "$30,000", "wheels": [ { "model": "K2Jk9848T", "location": "front-right" }, { "model": "MDJ29685DK", "location": "front-left" }, { "model": "KCMFD3415K", "location": "rear-right" }, { "model": "JEDH34231KK", "location": "rear-left" } ] }
}
JSON Query : $.car.wheels[*].model
Exercice 6
How would you display BMW ?
[ "Audi", "Alpha Romeo", "BMW", "Renault", "Mercedes",
]
JSON Query : $.[2]
Exercice 7
How would you display BMW and Mercedes only ?
[ "Audi", "Alpha Romeo", "BMW", "Renault", "Mercedes", ]
JSON Query : $.[2,4]
Exercice 8
How would you display BMW and Mercedes only ?
[ "Audi", "Alpha Romeo", "BMW", "Renault", "Mercedes", ]
JSON Query : $.[2,4]
Exercice 9
How would you display Alpha Romeo, BMW and Renault only ?
[ "Audi", "Alpha Romeo", "BMW", "Renault", "Mercedes", ]
JSON Query : $.[1:3]
Exercice 10 – Kubernetes 🙂
How would you get the image of the container ?
{ "apiVersion": "v1", "kind": "Pod", "metadata": { "name": "nginx-pod", "namespace": "default" }, "spec": { "containers": [ { "image": "nginx:alpine", "name": "nginx" } ], "nodeName": "node01" }
}
JSON Query : $.spec.containers[0].image
That’s it !
Sources :
wikipedia
yaml.org
json.org