EvaluateJsonPath
概述
通过设置JSONPath属性参数来提取输入流中的JSON键值对。
提取JSON字段值
通过设置属性,支持标准的JSONPath表达式:
- $.key.subKey
- $.key['subKey'] 或$.key["subKey"]
- 数组索引和范围,比如$.arrKey[0] 第一个,$.arrKey[1,2] 第二和三个,$.arrKey[-2:] 倒数两个
- 函数表达式, min()、max()、avg()、length()
- 过滤表达式,比如:$.products[?(@.color == ‘blue’)] or [?(@.color == “blue”)]
示例
样本数据
{
"customer": {
"groupId": "123",
"names": [
"Zhang San",
"Li Si",
"Wang Wu"
],
"details": {
"status": "OK"
}
}
}
提取属性设置
提取属性结果
详细的提取结果属性列表:
details
{"status":"OK"}
details_len
1
groupId
123
name1
Zhang San
name23
["Li Si","Wang Wu"]
names
["Zhang San","Li Si","Wang Wu"]
names_1
["Zhang San","Li Si","Wang Wu"]
names_len
3
ok_status
[{"status":"OK"}]
status
["OK"]
从中可以看出:
- 如果JSONPath表达式指向JSON对象或数组,则参数“返回类型”需修改为
json
,默认是自动,将导致提取失败。 - 数组提取,支持索引,比如“$.customer.names[0]”,返回结果为字面值。
- 数组范围,返回的结果也为数组,比如“$.customer.names[1,2]”
- 数组全量,返回结果数组,表达式可忽略"",即“$.customer.names.”与“$.customer.names”相同。
- 递归表达式,始终返回的是数组,比如“$..status”。
- 支持JSONPath的函数表达式,max,min,length,avg等,比如“$.customer.details.length()” 和“$.customer.names.length()”。
- 支持过滤表达式,比如“$.customer.details[?(@.status =~/ok/i)]”,返回匹配的结果,且为对象数组。