JSONPath 在k8s中使用
JSONPath 模板由 {} 包起来的 JSONPath 表达式组成。 除了原始的 JSONPath 语法之外,我们还添加了三个函数:
$
运算符是可选的,因为表达式默认情况下始终从根对象开始。- 可以使用
""
来引用 JSONPath 表达式中的文本。 - 可以使用
range
运算符来遍历列表。 - 可以使用负切片索引来反向遍历列表。负索引并不会遍历完列表。它们只需要满足
-index + listLength >= 0
便可执行。
结果对象使用 String() 函数打印。
给定输入:
{
"kind": "List",
"items":[
{
"kind":"None",
"metadata":{"name":"127.0.0.1"},
"status":{
"capacity":{"cpu":"4"},
"addresses":[{"type": "LegacyHostIP", "address":"127.0.0.1"}]
}
},
{
"kind":"None",
"metadata":{"name":"127.0.0.2"},
"status":{
"capacity":{"cpu":"8"},
"addresses":[
{"type": "LegacyHostIP", "address":"127.0.0.2"},
{"type": "another", "address":"127.0.0.3"}
]
}
}
],
"users":[
{
"name": "myself",
"user": {}
},
{
"name": "e2e",
"user": {"username": "admin", "password": "secret"}
}
]
}
函数 | 描述 | 示例 | 结果 |
---|---|---|---|
text | 纯文本 | kind is {.kind} | kind 是 List |
@ | 当前对象 | {@} | 与输入相同 |
. or [] | 子运算符 | {.kind} or {[‘kind’]} | List |
.. | 递归下降 | {..name} | 127.0.0.1 127.0.0.2 myself e2e |
* | 通配符,获取所有对象 | {.items[*].metadata.name} | [127.0.0.1 127.0.0.2] |
[start:end :step] | 下标运算符 | {.users[0].name} | myself |
[,] | 并集运算符 | {.items[*][‘metadata.name’, ‘status.capacity’]} | 127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8] |
?() | 过滤器 | {.users[?(@.name==“e2e”)].user.password} | secret |
range, end | 遍历列表 | {range .items[*]}[{.metadata.name}, {.status.capacity}] {end} | [127.0.0.1, map[cpu:4]] [127.0.0.2, map[cpu:8]] |
“ | 引用解释字符串 | {range .items[*]}{.metadata.name}{’\t’}{end} | 127.0.0.1 127.0.0.2 |
下面是使用 jsonpath 的一些例子:
$ kubectl get pods -o json
$ kubectl get pods -o=jsonpath='{@}'
$ kubectl get pods -o=jsonpath='{.items[0]}'
$ kubectl get pods -o=jsonpath='{.items[0].metadata.name}'
$ kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
在 Windows 上,你必须要 双 引号(而不是上面 bash 例子中的单引号)来引用包含空格符的任何 JSONPath 模板。 这也意味着,如果你要用字面值,必须要使用单引号或者转义双引号。例如:
C:\> kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{'\t'}{.status.startTime}{'\n'}{end}"
C:\> kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{\"\t\"}{.status.startTime}{\"\n\"}{end}"
k8s 示例
统计分区kube-system下状态为Running的Pod中镜像名称
kubectl get pods -o=jsonpath="{range .items[*]}{.status.phase=="Running"}{\"\t\"}{.spec.containers[*].image}{\"\n\"}{end}" -n kube-system
JsonPath介绍
JsonPath的来源
看它的名字你就能知道,这家伙和JSON文档有关系,正如XPath之于XML文档一样,JsonPath为Json文档提供了解析能力,通过使用JsonPath,你可以方便的查找节点、获取想要的数据,JsonPath是Json版的XPath。
JsonPath语法
JsonPath的语法相对简单,它采用开发语言友好的表达式形式,如果你了解类C语言,对JsonPath就不会感到不适应。
JsonPath语法要点:
$
表示文档的根元素@
表示文档的当前元素.node_name
或['node_name']
匹配下级节点[index]
检索数组中的元素[start:end:step]
支持数组切片语法*
作为通配符,匹配所有成员..
子递归通配符,匹配成员的所有子元素(<expr>)
使用表达式?(<boolean expr>)
进行数据筛选
下表将列举所有支持的语法,并对XPath进行比较:
XPath | JsonPath | 说明 |
---|---|---|
/ | $ | 文档根元素 |
. | @ | 当前元素 |
/ | . 或[] | 匹配下级元素 |
.. | N/A | 匹配上级元素,JsonPath不支持此操作符 |
// | .. | 递归匹配所有子元素 |
* | * | 通配符,匹配下级元素 |
@ | N/A | 匹配属性,JsonPath不支持此操作符 |
[] | [] | 下标运算符,根据索引获取元素,XPath索引从1开始,JsonPath索引从0开始 |
| | [,] | 连接操作符,将多个结果拼接成数组返回,可以使用索引或别名 |
N/A | [start:end:step] | 数据切片操作,XPath不支持 |
[] | ?() | 过滤表达式 |
N/A | () | 脚本表达式,使用底层脚本引擎,XPath不支持 |
() | N/A | 分组,JsonPath不支持 |
注意:
- JsonPath的索引从0开始计数
- JsonPath中字符串使用单引号表示,例如:
$.store.book[?(@.category=='reference')]
中的'reference'
JsonPath示例
下面是相应的JsonPath的示例,代码来源于https://goessner.net/articles/JsonPath/,JSON文档如下:
{
"store": {
"book": [{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
}, {
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}, {
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}, {
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
接下来我们看一下如何对这个文档进行解析:
XPath | JsonPath | Result |
---|---|---|
/store/book/author | $.store.book[*].author | 所有book的author节点 |
//author | $..author | 所有author节点 |
/store/* | $.store.* | store下的所有节点,book数组和bicycle节点 |
/store//price | $.store..price | store下的所有price节点 |
//book[3] | $..book[2] | 匹配第3个book节点 |
//book[last()] | $..book[(@.length-1)] ,或 $..book[-1:] | 匹配倒数第1个book节点 |
//book[position()<3] | $..book[0,1] ,或 $..book[:2] | 匹配前两个book节点 |
//book[isbn] | $..book[?(@.isbn)] | 过滤含isbn字段的节点 |
//book[price<10] | $..book[?(@.price<10)] | 过滤price<10 的节点 |
//* | $..* | 递归匹配所有子节点 |
你可以在http://jsonpath.com/站点进行验证JsonPath的执行效果。
参考链接:
https://v1-13.docs.kubernetes.io/zh/docs/reference/kubectl/jsonpath/