1. JSONPath简介
JSON (JavaScript Object Notation) allows for easy interchange of data, often between a program and a database.
JSONPath is a query language for JSON, similar to XPath for XML.
如上,json是一种常用的数据格式,jsonpath是json的查询语言,类似于XPath和SQL。
jsonpath在线测试:JSONPath Online Evaluator
2. YAML和JSON
YAML和JSON可以相互转化,详情参考《YAML语言》。
3. JSONPath语法
3.1. 操作符
Operator | Description |
---|---|
$ | The root element to query. This starts all path expressions. |
@ | The current node being processed by a filter predicate. |
* | Wildcard. Available anywhere a name or numeric are required. |
.. | Deep scan. Available anywhere a name is required. |
.<name> | Dot-notated child |
['<name>' (, '<name>')] | Bracket-notated child or children |
[<number> (, <number>)] | Array index or indexes |
[start:end] | Array slice operator |
[?(<expression>)] | Filter expression. Expression must evaluate to a boolean value. |
3.2. 函数
Functions can be invoked at the tail end of a path - the input to a function is the output of the path expression.
The function output is dictated by the function itself.
Function | Description | Output |
---|---|---|
min() | Provides the min value of an array of numbers | Double |
max() | Provides the max value of an array of numbers | Double |
avg() | Provides the average value of an array of numbers | Double |
stddev() | Provides the standard deviation value of an array of numbers | Double |
length() | Provides the length of an array | Integer |
sum() | Provides the sum value of an array of numbers | Double |
3.3. 过滤器
Filters are logical expressions used to filter arrays. A typical filter would be [?(@.age > 18)]
where @
represents the current item being processed. More complex filters can be created with logical operators &&
and ||
. String literals must be enclosed by single or double quotes ([?(@.color == 'blue')]
or [?(@.color == "blue")]
).
Operator | Description |
---|---|
== | left is equal to right (note that 1 is not equal to ‘1’) |
!= | left is not equal to right |
< | left is less than right |
<= | left is less or equal to right |
> | left is greater than right |
>= | left is greater than or equal to right |
=~ | left matches regular expression [?(@.name =~ /foo.*?/i)] |
in | left exists in right [?(@.size in [‘S’, ‘M’])] |
nin | left does not exists in right |
subsetof | left is a subset of right [?(@.sizes subsetof [‘S’, ‘M’, ‘L’])] |
anyof | left has an intersection with right [?(@.sizes anyof [‘M’, ‘L’])] |
noneof | left has no intersection with right [?(@.sizes noneof [‘M’, ‘L’])] |
size | size of left (array or string) should match right |
empty | left (array or string) should be empty |
3.4. DEMO
Given the json
1 | { |
JsonPath (click link to try) | Result |
---|---|
$.store.book[*].author | The authors of all books |
$..author | All authors |
$.store.* | All things, both books and bicycles |
$.store..price | The price of everything |
$..book[2] | The third book |
$..book[-2] | The second to last book |
$..book[0,1] | The first two books |
$..book[:2] | All books from index 0 (inclusive) until index 2 (exclusive) |
$..book[1:2] | All books from index 1 (inclusive) until index 2 (exclusive) |
$..book[-2:] | Last two books |
$..book[2:] | Book number two from tail |
$..book[?(@.isbn)] | All books with an ISBN number |
$.store.book[?(@.price < 10)] | All books in store cheaper than 10 |
$..book[?(@.price <= $[‘expensive’])] | All books in store that are not “expensive” |
$..book[?(@.author =~ /.*REES/i)] | All books matching regex (ignore case) |
$..* | Give me every thing |
$..book.length() | The number of books |
4. kubectl + JSONPath
kubelet支持JSONPath,具体参考JSONPath 支持。
除了标准jsonpath语法外,kubernetes jsonpath模板还额外支持以下语法:
- 用
""
双引号来引用JSONPath表达式中的文本 - 使用range和end来遍历集合
- 使用负数来从尾部索引集合
例如查看node的cpu信息:
1 | kubectl get nodes -o=jsonpath='{.items[*].metadata.name}' |
5. 特殊说明
如果key中包含点.
,例如key为config.yaml
,那么在使用jsonpath的时候需要对key中的点.
进行转义。
1 | kubectl get cm test-config -n test -o jsonpath='{.data.config\.yaml}' |