JSON Path Course Resource
JSON Path Course Resource
FOR BEGINNERS
© Copyright KodeKloud
2
YAML
Introduction
© Copyright KodeKloud
© Copyright KodeKloud
© Copyright KodeKloud
© Copyright KodeKloud
© Copyright KodeKloud
© Copyright KodeKloud
© Copyright KodeKloud
© Copyright KodeKloud
© Copyright KodeKloud
Check out our full JSON Path course here: https://kode.wiki/3NuVhVV
© Copyright KodeKloud
12
JSON PATH
PART – 1 - INTRODUCTION
© Copyright KodeKloud
Objectives
• YAML
• YAML vs JSON
• JSON PATH
• Dictionaries
• Lists
• Lists and Dictionaries
• Criteria
• Practice Exercises
© Copyright KodeKloud
YAML
Color Price
Blue $20,000
car:
color: blue
price: $20,000
© Copyright KodeKloud
YAML vs JSON
{
"car": { car:
"color": "blue", color: blue
"price": "$20,000" price: $20,000
}
}
© Copyright KodeKloud
YAML vs JSON
{
"car": { car:
"color": "blue", color: blue
"price": "$20,000", price: $20,000
"wheels": [ wheels:
{
- model: X345ERT
"model": "X345ERT",
"location": "front-right"
location: front-right
}, - model: X345ERT
{ location: front-left
"model": "X345ERT", - model: X345ERT
"location": "front-left" location: rear-right
}, - model: X345ERT
{ location: rear-right
"model": "X345ERT",
"location": "rear-right"
},
{
"model": "X345ERT",
"location": "rear-right"
}
]
}
} KodeKloud
© Copyright
© Copyright KodeKloud
JSON PATH
{
"car": { car:
"color": "blue", color: blue
"price": "$20,000" price: $20,000
}
}
© Copyright KodeKloud
Query
DAT QUERY RESULT
A
Get color and price of cars Color Price
1 select color,price from cars;
Car Color Price Year Blue $20,000
Red $22,000
1 Blue $20,000 1987
Yellow $18,000
© Copyright KodeKloud
JSON PATH - Dictionaries
DAT QUERY RESULT
A
{
{ Get car details "color": "blue",
"car": { "price": "$20,000"
"color": "blue", car
}
"price": "$20,000"
}
},
} "bus": {
Get bus details
"color": "white", {
"price": "$120,000" bus "color": "white",
} "price": "$120,000"
} }
car.color "blue“
bus.price “$120,000“
© Copyright KodeKloud
JSON PATH - Dictionaries
DAT QUERY RESULT
A
$. car.color
vehicles. "blue"
$. bus.price
vehicles. “$120,000"
© Copyright KodeKloud
JSON PATH - Dictionaries
DAT QUERY RESULT
A
[
{
"color": "blue",
Get car details
{ "price": "$20,000"
"vehicles": { $.vehicles.car }
"car": { ]
"color": "blue",
"price": "$20,000" [
}, Get bus details {
"bus": { "color": "white",
$.vehicles.bus "price": "$120,000"
"color": "white",
"price": "$120,000" }
} ]
} Get car’s color
} [
$.vehicles.car.color "blue"
]
[
0 "car",
Get the 1st element
1 "bus",
2 [ "car“ ]
"truck", $[0]
3 "bike"
]
Get the 4th element
[ “bike“ ]
$[3]
© Copyright KodeKloud
JSON PATH – Dictionary & Lists
DAT QUERY RESULT
A
{
"car": {
"color": "blue",
"price": "$20,000",
"wheels": [ [
{ {
"model": "X345ERT", "model": "X345ERT",
"location": "front-right" "location": "front-right"
}, },
Get the model of the 2nd wheel
{ {
"model": "X346GRX", $.car.wheels[1].model "model": "X346GRX"
"X346GRX",
"location": "front-left" "location": "front-left"
}, },
}
{ {
"model": "X236DEM", "model": "X236DEM",
"location": "rear-right" "location": "rear-right"
}, },
{ {
"model": "X987XMV", "model": "X987XMV",
"location": "rear-right" "location": "rear-right"
} }
] ]
}
© Copyright KodeKloud
}
JSON PATH – Criteria
DAT QUERY RESULT
A
[
12,
43,
Get all numbers greater than 40 [
23,
43,
12, $[ Check if each item in the array > 40 ] 56,
56,
43,
43,
93,
Check if => ? () 93,
45,
32,
63,
45,
63,
$[ ?( each item in the list > 40 )] 78
]
27,
8,
78 each item in the list => @
]
$[ ?( @ > 40 )]
@ == 40 @ in [40,43,45]
@ != 40 @ nin [40,43,45]
© Copyright KodeKloud
JSON PATH – Criteria
DAT QUERY RESULT
A
{
"car": {
"color": "blue",
"price": "$20,000",
"wheels": [
{
"model": "X345ERT", Get the model of the rear-right wheel
"location": "front-right"
}, $.car.wheels[2].model
{
"model": "X346GRX", "X236DEM"
"location": "front-left"
},
{
"model": "X236DEM",
"location": "rear-right"
},
{
"model": "X987XMV",
"location": "rear-left"
}
]
}
© Copyright KodeKloud
}
JSON PATH – Criteria
DAT QUERY RESULT
A
{
"car": {
"color": "blue",
"price": "$20,000",
"wheels": [
{
"model": "X345ERT", Get the model of the rear-right wheel
"location": "front-right"
}, $.car.wheels[2].model
{
"model": "X236DEM",
"location": "rear-right" $.car.wheels[?( @.location == “rear-right” ) ].model "X236DEM"
},
{
"model": "X346GRX",
"location": "front-left"
},
{
"model": "X987XMV",
"location": "rear-left"
}
]
}
© Copyright KodeKloud
}
kodekloud.com/p/json-path-quiz
© Copyright KodeKloud
References
https://github.com/json-path/JsonPath
© Copyright KodeKloud
Check out our full JSON Path course here: https://kode.wiki/3NuVhVV
© Copyright KodeKloud
33
JSON PATH
PART – 2 – WILD CARD
© Copyright KodeKloud
JSON PATH – Wildcard
DAT QUERY RESULT
A
[
{ Get 1st wheel’s model [ "X345ERT" ]
"model": "X345ERT", $[0].model
"location": "front-right"
},
{
"model": "X346ERT", Get 4th wheel’s model
"location": "front-left" [ "X348ERT" ]
}, $[4].model
{
"model": "X347ERT",
"location": "rear-right"
}, Get all wheels’ model [ "X345ERT", "X346ERT",
{ $[*].color "X347ERT", "X348ERT" ]
"model": "X348ERT",
"location": "rear-right"
}
]
© Copyright KodeKloud
JSON PATH – Wildcard
DAT QUERY RESULT
{
A
"car": {
"color": "blue",
"price": 20000,
"wheels": [ Get car’s 1st wheel model
{ [ "X345ERT“ ]
"model": "X345ERT", $.car.wheels[0].model
},
{
"model": "X346ERT",
} Get car’s all wheel model [ "X345ERT", "X346ERT" ]
]
},
$.car.wheels[*].model
"bus": {
"color": "white",
"price": 120000,
Get bus’s wheel models
"wheels": [ [ "Z227KLJ“, "Z226KLJ" ]
{ $.bus.wheels[*].model
"model": "Z227KLJ",
},
{
"model": "Z226KLJ",
Get all wheels’ models [ "X345ERT", "X346ERT",
}
] $.*.wheels[*].model "Z227KLJ", "Z226KLJ" ]
}
© Copyright KodeKloud
}
Check out our full JSON Path course here: https://kode.wiki/3NuVhVV
© Copyright KodeKloud
38
JSON PATH
PART – 3 – LISTS
© Copyright KodeKloud
JSON PATH - Lists
DAT QUERY RESULT
A Get the 1st element
[ [ “Apple“ ]
"Apple",
$[0]
"Google",
"Microsoft",
Get the 4th element [ “Amazon“ ]
"Amazon",
"Facebook", $[3]
"Coca-Cola",
"Samsung", Get the 1st and 4th element [ “Apple”, “Amazon“]
"Disney",
"Toyota", $[0,3]
"McDonald's"
] Get the 1st to 4th element [
"Apple",
$[0:3] "Google",
"Microsoft"
START : END ]
[
$[0:4] "Apple",
"Google",
"Microsoft",
© Copyright KodeKloud
"Amazon"
]
JSON PATH - Lists
DAT QUERY RESULT
A
[ $[0:8] [
"Apple", "Apple",
"Google", "Google",
"Microsoft", START : END "Microsoft",
"Amazon", "Amazon",
"Facebook",
"Facebook",
$[0:8:2] "Coca-Cola",
"Coca-Cola", "Samsung",
"Samsung", "Disney"
"Disney", ]
"Toyota", START : END : STEP
"McDonald's"
]
[
"Apple",
"Microsoft",
"Facebook",
"Samsung"
]
© Copyright KodeKloud
JSON PATH - Lists
DAT QUERY RESULT
A Get the last element [
[
0 "Apple", -10 "McDonald’s”
$[9] ]
1 "Google", -9
2 "Microsoft", -8
3 "Amazon", -7
4 "Facebook", -6 Get the last element
5 "Coca-Cola", -5 Does not work in certain
6 -4 $[-1] implementations
"Samsung",
7 "Disney", -3
8 "Toyota", -2
$[-1:0]
9 "McDonald's" -1
]
$[-1:]
[
"Apple",
"Microsoft", Get the last 3 elements [
"Facebook", "Disney",
"Samsung" $[-3:] "Toyota",
] "McDonald's"
]
© Copyright KodeKloud
Check out our full JSON Path course here: https://kode.wiki/3NuVhVV
© Copyright KodeKloud
43
JSON PATH
IN KUBERNETES
© Copyright KodeKloud
Objectives
© Copyright KodeKloud
Pre-Requisite
www.kodekloud.com/p/json-path-quiz
© Copyright KodeKloud
Why JSON PATH?
© Copyright KodeKloud
KubeCtl
kubectl get nodes
kube-
apiserver
Kubectl
© Copyright KodeKloud
KubeCtl - JSON PATH
NAME CPU NAME TAINTS NAME ARCHITECTURE
master 4 master node-role.kubernetes.io/master master amd64
node01 4 node01 node01 amd64
NAME IMAGE
red nginx
blue ubuntu
yellow redis
© Copyright KodeKloud
How to JSON PATH in KubeCtl?
kubectl get nodes -o json
4
]
Use the JSON PATH query with kubectl command }
{.items[*].status.capacity.cpu}
kubectl get nodes -o=jsonpath='{.items[*].status.capacity.cpu}'
4 4
© Copyright KodeKloud
Loops - Range
kubectl get pods -o=jsonpath='{.items[*].metadata.name} {"\n"} {.items[*].status.capacity.cpu}'
© Copyright KodeKloud
JSON PATH for Custom Columns
kubectl get nodes -o=jsonpath='{.items[*].metadata.name}{"\n"} {.items[*].status.capacity.cpu}'
NODE CPU
master node01
master 4
4 4
node01 4
© Copyright KodeKloud
Check out our full JSON Path course here: https://kode.wiki/3NuVhVV
© Copyright KodeKloud