You are on page 1of 57

JSON PATH

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

2 Red $22,000 1988


Get blue car details
Car Color Price Year

2 select * from cars 1 Blue $20,000 1987


3 Yello $18,000 1989 where color is "Blue";
w

Get price of the blue car Price


3 select price from cars
$20,000
where color is "Blue";
© Copyright KodeKloud
JSON PATH
DAT QUERY RESULT
A

{ Get car details {


"car": { "color": "blue",
"color": "blue", car
"price": "$20,000"
"price": "$20,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"
} }

Get car’s color

car.color "blue“

Get bus’s color

bus.price “$120,000“
© Copyright KodeKloud
JSON PATH - Dictionaries
DAT QUERY RESULT
A

Get car details {


{
"color": "blue",
"vehicles": { vehicles.car "price": "$20,000"
"car": {
}
"color": "blue",
"price": "$20,000"
}, Get bus details {
"bus": { "color": "white",
vehicles.bus
"color": "white", "price": "$120,000"
"price": "$120,000" }
}
} Get car’s color
}
vehicles. car.color "blue"

Get bus’s color

vehicles. bus.price “$120,000"


© Copyright KodeKloud
Root element
QUERY RESULT

{ Get car details {


"car": { "color": "blue",
"color": "blue", $.car
vehicles. "price": "$20,000"
"price": "$20,000" }
},
"bus": {
Get bus details {
"color": "white",
"color": "white",
"price": "$120,000" $.bus
vehicles.
"price": "$120,000"
}
}
}

Get car’s color

$. car.color
vehicles. "blue"

Get bus’s color

$. 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"
]

Get bus’s color


[
$.vehicles.bus.price “$120,000"
© Copyright KodeKloud ]
JSON PATH - Lists
DAT QUERY RESULT
A

[
0 "car",
Get the 1st element
1 "bus",
2 [ "car“ ]
"truck", $[0]
3 "bike"
]
Get the 4th element
[ “bike“ ]
$[3]

Get the 1st and 4th element


[ “car”, “bike“]
$[0,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 car’s color [ "blue" ]


"car": {
"color": "blue", $.car.color
"price": "$20,000"
}
},
} "bus": {
Get bus’s color
"color": "white", [ “white" ]
"price": "$120,000" $.bus.color
}
}

Get all colors


[ "blue", "white" ]
$.*.color

Get all prices


[ "$20,000", "$120,000" ]
$.*.price
© 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

• JSON PATH in KubeCtl


• Why JSON PATH?
• View and interpret KubeCtl output in JSON Format
• How to use JSON PATH with KubeCtl
• JSON PATH Examples
• Loops – Range
• Custom Columns
• Sort
• Practice Tests and Exercises

© Copyright KodeKloud
Pre-Requisite

• JSON PATH for Beginners on Youtube


• JSON PATH Practice Tests on KodeKloud
• JSON PATH Practice Tests on Kubernetes data set on KodeKloud

www.kodekloud.com/p/json-path-quiz

© Copyright KodeKloud
Why JSON PATH?

• Large Data sets


• 100s of Nodes
• 1000s of PODs, Deployments, ReplicaSets

© Copyright KodeKloud
KubeCtl
kubectl get nodes

kube-
apiserver

Kubectl

NAME STATUS ROLES AGE VERSION {


"apiVersion": "v1",
master Ready master 40m v1.11.3 "items": [
{
node01 Ready <none> 40m v1.11.3 "apiVersion": "v1",
"kind": "Node",
"metadata": {
"annotations": {
"node.alpha.kubernetes.io/ttl": "0",
},
"creationTimestamp": "2019-06-21T09:01:56Z",
"labels": {
"beta.kubernetes.io/arch": "amd64",
"beta.kubernetes.io/os": "linux",
"kubernetes.io/hostname": "master",
"node-role.kubernetes.io/master": ""
}
}
}
]
}
© Copyright KodeKloud
KubeCtl
NAME STATUS ROLES AGE VERSION {
"apiVersion": "v1",
master Ready master 40m v1.11.3 "items": [
{
node01 Ready <none> 40m v1.11.3 "apiVersion": "v1",
"kind": "Node",
"metadata": {
"annotations": {
"node.alpha.kubernetes.io/ttl": "0",
},
"creationTimestamp": "2019-06-21T09:01:56Z",
"labels": {
"beta.kubernetes.io/arch": "amd64",
"beta.kubernetes.io/os": "linux",
"kubernetes.io/hostname": "master",
"node-role.kubernetes.io/master": ""
}
}
}
kubectl get nodes –o wide ]
}

NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KE


master Ready master 7m v1.11.3 172.17.0.44 <none> Ubuntu 16.04.2 LTS 4.
node01 Ready <none> 6m v1.11.3 172.17.0.63 <none> Ubuntu 16.04.2 LTS 4.

© 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

kubectl get pods -o json

1 Identify the kubectl command


{
"apiVersion": "v1",
"kind": "List",
"items": [
{

2 Familiarize with JSON output


"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "nginx-5557945897-gznjp",
},
"spec": {
"containers": [

3 Form the JSON PATH query


{
"image": "nginx:alpine",
"name": "nginx"
}
.items[0].spec.containers[0].image ],
"nodeName": "node01"
}
}

4
]
Use the JSON PATH query with kubectl command }

kubectl get pods –o=jsonpath=‘{ }’


© Copyright KodeKloud
JSON PATH Examples {
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
kubectl get nodes -o=jsonpath='{.items[*].metadata.name}' "kind": "Node",
"metadata": {
master node01 "name": "master"
},
"status": {
"capacity": {
kubectl get nodes -o=jsonpath='{.items[*].status.nodeInfo.architecture}' "cpu": "4"
},
amd64 amd64 "nodeInfo": {
"architecture": "amd64",
"operatingSystem": "linux",
}
}
},
kubectl get pods -o=jsonpath='{.items[*].status.capacity.cpu}' {
"apiVersion": "v1",
4 4 "kind": "Node",
"metadata": {
"name": "node01",
},
"status": {
"capacity": {
"cpu": "4",
},
"nodeInfo": {
"architecture": "amd64",
"operatingSystem": "linux",
}
}
}
© Copyright KodeKloud ],
"kind": "List",
}
JSON PATH Examples
kubectl get nodes -o=jsonpath='{.items[*].metadata.name}'
master node01
{"\n"} New line
kubectl get nodes -o=jsonpath='{.items[*].status.nodeInfo.architecture}'
{"\t"} Ta
amd64 amd64 b

{.items[*].status.capacity.cpu}
kubectl get nodes -o=jsonpath='{.items[*].status.capacity.cpu}'
4 4

kubectl get nodes -o=jsonpath='{.items[*].metadata.name} '


master node01 4 4

kubectl get nodes -o=jsonpath='{.items[*].metadata.name}{.items[*].status.capacity.cpu}'


{"\n"}
master node01
© Copyright
4 KodeKloud
4
Loops - Range
kubectl get nodes -o=jsonpath='{.items[*].metadata.name}{"\n"} {.items[*].status.capacity.cpu}'

master node01 master 4


4 4 node01 4

FOR EACH NODE '{range .items[*]}

PRINT NODE NAME \t PRINT CPU COUNT \n


{.metadata.name} {"\t"} {.status.capacity.cpu} {"\n"}
END FOR
{end}'

© Copyright KodeKloud
Loops - Range
kubectl get pods -o=jsonpath='{.items[*].metadata.name} {"\n"} {.items[*].status.capacity.cpu}'

kubectl get nodes -o=jsonpath=

'{range .items[*]} {.metadata.name} {"\t"} {.status.capacity.cpu} {"\n"} {end}'

© 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

kubectl get nodes -o=custom-columns=<COLUMN NAME>:<JSON PATH>

kubectl get nodes -o=custom-columns=NODE:.metadata.name ,CPU:.status.capacity.cpu


NODE CPU
master 4
node01 4
© Copyright KodeKloud
JSON PATH for Sort
.metadata.name ,CPU:.status.capacity.cpu
kubectl get nodes -o=custom-columns=NODE:.metadata.name .status.capacity.cpu
NODE CPU
master 4
node01 4

kubectl get nodes --sort-by=


NAME STATUS ROLES AGE VERSION
master Ready master 5m v1.11.3
node01 Ready <none> 5m v1.11.3

kubectl get nodes --sort-by=


NAME STATUS ROLES AGE VERSION
master Ready master 5m v1.11.3
node01 Ready <none> 5m v1.11.3

© Copyright KodeKloud
Check out our full JSON Path course here: https://kode.wiki/3NuVhVV

© Copyright KodeKloud

You might also like