本节详细介绍监控项值的预处理步骤中如何使用JSONPath功能。
JSONPath是由点分隔的段,每段可以是一个简单的单词,如JSON key的名称,星号*或者也可以是括在[]中的更复杂的结构。括号段之前的分隔点是可选的,可以省略。例如:
| 路径 描 | |
|---|---|
$.object.name | 返回object.name的值 |
$.object['name'] | 返回object.name的值 |
$.object.['name'] | 返回object.name的值 |
$["object"]['name'] | 返回object.name的值 |
$.['object'].["name"] | 返回object.name的值 |
$.object.history.length() | 返回数组object.history的长度 |
$[?(@.name == 'Object')].price.first() | 返回名称为'Object'的第一个对象的price字段 |
$[?(@.name == 'Object')].history.first().length() | 返回名称为'Object'的第一个对象history字段数组的长度 |
$[?(@.price > 10)].length() | 返回price字段大于10对象数量 |
| 格式 描 | |
|---|---|
<name> | 通过name匹配对象属性 |
* | 匹配所有对象属性 |
['<name>'] | 通过name匹配对象属性 |
['<name>', '<name>', ...] | 通过列出的name列表,匹配对象属性 |
[<index>] | 通过索引匹配数组元素 |
[<number>, <number>, ...] | 通过索引列表匹配数组元素 |
[*] | 匹配所有对象属性或数组元素 |
[<start>:<end>] | 通过定义范围来匹配数组元素: <start> - 要匹配的第一个索引(包括)。如果未指定,则从头开始匹配所有数组元素。如果为负,则指定从数组末尾开始的偏移量。 <end> - 最后要匹配的索引(不包括)。如果未指定,则将所有数组元素匹配到末尾。如果为负,则指定从数组末尾开始的偏移量。 |
[?(<expression>)] | 通过应用过滤器表达式来匹配对象/数组元素 |
To find a matching segment ignoring its ancestry (detached segment) it must be prefixed with '..',例如$..name或者$..['name'] 返回所有name属性的值
可以通过向 JSONPath 添加"~"后缀来提取匹配的元素名称。它返回匹配对象的名称或匹配数组项的字符串格式的索引。输出格式遵循与其他 JSONPath 查询相同的规则 - 以"as is"形式返回确定路径结果,并以数组形式返回不确定路径结果。但是,提取与特定路径匹配的元素的名称没有意义。
筛选表达式是固定符号的一种表达式。
支持的操作:
| 操作 描 | 例子 | |
|---|---|---|
"<text>"'<text>' | Text变量. ' | alue: \'1\'' "value: '1'" |
<number> | 支持科学计数法的变量 123 | |
<jsonpath starting with $> | JSONPath 从输入文档根节点引用的值;仅支持确定路径 $.object.name | |
<jsonpath starting with @> | JSONPath 从当前对象/元素引用的值;仅支持确定路径。 @.name | |
支持的运算符:
| 运算符 类型 | 描述 | 结果 | |
|---|---|---|---|
- | binary | 减法 数 | |
+ | binary | 加法 数 | |
/ | binary | 除法 数 | |
* | binary | 乘法 数 | |
== | binary | 等于 布 | 值 (1 or 0) |
!= | binary | 不等于 布尔 | (1 or 0) |
< | binary | 小于 布 | 值 (1 or 0) |
<= | binary | 小于或等于 布尔值 | 1 or 0) |
> | binary | 大于 布 | 值 (1 or 0) |
>= | binary | 大于或等于 布尔值 | 1 or 0) |
=~ | binary | 匹配正则 布尔值 | (1 or 0) |
! | unary | 布尔值非 布尔值 | (1 or 0) |
\|\| | binary | 布尔值或 布尔值 | (1 or 0) |
&& | binary | 布尔值且 布尔值 | (1 or 0) |
可以在JSONPath的末尾使用函数。如果前一个函数返回后一个函数接受的值,则可以链接多个函数。 支持的函数:
| 函数 描 | 输入 | 输出 | |
|---|---|---|---|
avg | 输入数组中数字的平均值 数字数组 数字 | ||
min | 输入数组中数字的最小值 数字数组 数字 | ||
max | 输入数组中数字的最大值 数字数组 数字 | ||
sum | 输入数组中的数字总和 数字数组 数字 | ||
length | 输入数组中的元素数 数组 | 数字 | |
first | 第一个数组元素 数组 | 取决于输入数组内 | 的JSON构造(对象,数组,值) |
JSONPath聚合函数接受带引号的数字值。这意味着如果需要聚合,则将值从字符串类型转换为数字。 输入不兼容将导致函数产生错误。
JSONPath可以分为确定路径和不确定路径。确定定路径只能返回null或单个匹配项。不确定路径可以返回多个匹配项,基本上是具有分离的JSONPath,多个名称/索引列表,数组切片或表达式段。但是,使用函数时,JSONPath变得确定,因为函数始终输出单个值。 确定路径返回其引用的对象/数组/值,而不确定路径返回匹配的对象/数组/值的数组。
空值 (空格, tab 字符) 可以在括号符号段和表达式中自由使用,例如, $[ 'a' ][ 0 ][ ?( $.b == 'c' ) ][ : -1 ].first( )。
字符串应该用单引号'或双引号"括起来。在字符串中,单引号或双引号(取决于用于将其括起来的引号)和反斜杠“\”用反斜杠\字符进行转义。
{ "books": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95, "id": 1 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99, "id": 2 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99, "id": 3 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99, "id": 4 } ], "services": { "delivery": { "servicegroup": 1000, "description": "Next day delivery in local town", "active": true, "price": 5 }, "bookbinding": { "servicegroup": 1001, "description": "Printing and assembling book in A5 format", "active": true, "price": 154.99 }, "restoration": { "servicegroup": 1002, "description": "Various restoration methods", "active": false, "methods": [ { "description": "Checmical cleaning", "price": 46 }, { "description": "Pressing pages damaged by moisture", "price": 24.5 }, { "description": "Rebinding torn book", "price": 99.49 } ] } }, "filters": { "price": 10, "category": "fiction", "no filters": "no \"filters\"" }, "closed message": "Store is closed", "tags": [ "a", "b", "c", "d", "e" ] }| JSONPath | 类型 结 | 注释 | |
|---|---|---|---|
$.filters.price | 明确的 10 | ||
$.filters.category | 明确的 fi | tion | |
$.filters['no filters'] | 明确的 no | "filters" | |
$.filters | 明确的 { | <"price": 10, "category": "fiction", "no filters": "no \"filters\"" } | |
$.books[1].title | 明确的 Sw | rd of Honour | |
$.books[-1].author | 明确的 J. | R. R. Tolkien | |
$.books.length() | 明确的 4 | ||
$.tags[:] | 不确定的 [" | ", "b", "c", "d", "e" ] | |
$.tags[2:] | 不确定的 [" | ", "d", "e" ] | |
$.tags[:3] | 不确定的 [" | ", "b", "c"] | |
$.tags[1:4] | 不确定的 [" | ", "c", "d"] | |
$.tags[-2:] | 不确定的 [" | ", "e"] | |
$.tags[:-3] | 不确定的 [" | ", "b"] | |
$.tags[:-3].length() | 不确定的 2 | ||
$.books[0, 2].title | 不确定的 [" | ayings of the Century", "Moby Dick"] | |
$.books[1]['author', "title"] | 不确定的 [" | velyn Waugh", "Sword of Honour"] | |
$..id | 不确定的 [1 | 2, 3, 4] | |
$.services..price | 不确定的 [5 | 154.99, 46, 24.5, 99.49] | |
$.books[?(@.id == 4 - 0.4 * 5)].title | 不确定的 [" | word of Honour"] 这个查 | 表明可以在查询中使用算术操作。当然,这个查询可以简化为 $.books[?(@.id == 2)].title |
$.books[?(@.id == 2 \|\| @.id == 4)].title | 不确定的 [" | word of Honour", "The Lord of the Rings"] | |
$.books[?(!(@.id == 2))].title | 不确定的 [" | ayings of the Century", "Moby Dick", "The Lord of the Rings"] | |
$.books[?(@.id != 2)].title | 不确定的 [" | ayings of the Century", "Moby Dick", "The Lord of the Rings"] | |
$.books[?(@.title =~ " of ")].title | 不确定的 [" | ayings of the Century", "Sword of Honour", "The Lord of the Rings"] | |
$.books[?(@.price > 12.99)].title | 不确定的 [" | he Lord of the Rings"] | |
$.books[?(@.author > "Herman Melville")].title | 不确定的 [" | ayings of the Century", "The Lord of the Rings"] | |
$.books[?(@.price > $.filters.price)].title | 不确定的 [" | word of Honour", "The Lord of the Rings"] | |
$.books[?(@.category == $.filters.category)].title | 不确定的 [" | word of Honour","Moby Dick","The Lord of the Rings"] | |
$..[?(@.id)] | 不确定的 [ | <{ "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95, "id": 1 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99, "id": 2 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99, "id": 3 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99, "id": 4 } ] | |
$.services..[?(@.price > 50)].description | 不确定的 '[ | Printing and assembling book in A5 format", "Rebinding torn book"] | |
$..id.length() | 明确的 4 | ||
$.books[?(@.id == 2)].title.first() | 明确的 Sw | rd of Honour | |
$..tags.first().length() | 明确的 5 | $. | tags is indefinite path, so it returns an array of matched elements - [["a", "b", "c", "d", "e" ]], first() returns the first element - ["a", "b", "c", "d", "e" ] and finally length() calculates its length - 5. |
$.books[*].price.min() | 明确的 8. | 5 | |
$..price.max() | 明确的 15 | .99 | |
$.books[?(@.category == "fiction")].price.avg() | 明确的 14 | 99 | |
$.books[?(@.category == $.filters.xyz)].title | 不确定的 | 对于确 | 路径和不确定路径,不匹配的查询返回NULL |
$.services[?(@.active=="true")].servicegroup | 不确定的 [1 | 00,1001] 文本常 | 必须在布尔值比较中使用 |
$.services[?(@.active=="false")].servicegroup | 不确定的 [1 | 02] 文本常 | 必须在布尔值比较中使用 |
$.services[?(@.servicegroup=="1002")]~.first() | 明确的 re | toration | |