技术栈:kettle中json input中数据的解析

发布时间:2025-08-17 01:40  浏览量:1

本文详细介绍了jsonPath的使用,包括其基本语法、表达式、通配符及过滤器。通过具体例子展示了如何选取JSON数据中的特定元素,如选取所有学历的name、过滤价格低于10的书籍等。此外,还列举了各种操作符,如等于、大于、正则匹配等,用于筛选数据。最后,提供了在线解析器帮助读者实践和测试JSONPath表达式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

展开

类似于XPath在xml文档中的定位,JsonPath表达式通常是用来路径检索或设置Json的。其表达式可以接受“dot–notation”和“bracket–notation”格式,例如$.store.book[0].title、$[‘store’][‘book’][0][‘title’]

JSONPath 表达式JSONPaht 用一个抽象的名字$来表示最外层对象。使用.符号:$.store.book[0].title使用:$['store']['book'][0]['title']数组索引

1)JSONPath 允许使用通配符 * 表示所以的子元素名和数组索引。还允许使用 '..' 从E4X参照过来的和数组切分语法[start:end:step]

2)$.store.book[(@.length-1)].title

3)使用'@'符号表示当前的对象,?() 使用逻辑表达式来过滤

$.store.book[?(@.price

XPathJSONPathDescription/$表示根元素.@当前元素/. or 子元素..n/a父元素//..递归下降,JSONPath是从E4X借鉴的。**通配符,表示所有的元素@n/a属性访问字符子元素操作符|连接操作符在XPath 结果合并其它结点集合。JSONP允许name或者数组索引。n/a[start:end:step]数组分割操作从ES4借鉴。?应用过滤表示式n/a脚本表达式,使用在脚本引擎下面。n/aXpath分组

接口返回:

[{

"id": "PRIMARY",

"name": "小学","front_id": "PRIMARY",}, {"id": "JUNIOR","name": "初中","front_id": "JUNIOR","front_name": "初中"}, {"id": "HIGH","name": "高中","front_id": "HIGH",}, {"id": "TECHNICAL",}, {"id": "COLLEGE","name": "大专","front_id": "COLLEGE","front_name": "大专"}, {"id": "BACHELOR","name": "本科","front_id": "BACHELOR","front_name": "本科"}, {}]

AI写代码


JSONPath结果
$.[*].name所有学历的name
$.[*].id所有的id
$.[*]所有元素
$.[(@.length-2)].name倒数第二个元素的name
$.[2]第三个元素
$.[(@.length-1)]最后一个元素
$.[0,1]$.[:2]前面的两个元素
$.[?(@.name =~ /.*中/i)]过滤出所有的name包含“中”的书。
$..book[?(@.price过滤出价格低于10的书。
$.[*].length所有元素的个数

接口返回:

{

"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"price": 19.95}

AI写代码

JsonPath表达式结果$.store.book[*].author

$..author[
“Nigel Rees”,
“Evelyn Waugh”,
“Herman Melville”,
“J. R. R. Tolkien”
]

$.store.* 显示所有叶子节点值

[
[
{
”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
},
{

”author” : “Herman Melville”,
”title” : “Moby Dick”,
”isbn” : “0-553-21311-3”,
”price” : 8.99
},
{

”author” : “J. R. R. Tolkien”,
”title” : “The Lord of the Rings”,
”isbn” : “0-395-19395-8”,
”price” : 22.99
}
],
{
”color” : “red”,
”price” : 19.95
}
]

$.store..price[
8.95,
12.99,
8.99,
22.99,
19.95
]

$..book[0,1]

$..book[:2]

[
{
”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
}
]

$..book[-2:]获取最后两本书

$..book[2:]

[
{
”category” : “fiction”,
”author” : “Herman Melville”,
”title” : “Moby Dick”,
”isbn” : “0-553-21311-3”,
”price” : 8.99
},
{

”author” : “J. R. R. Tolkien”,
”title” : “The Lord of the Rings”,
”isbn” : “0-395-19395-8”,
”price” : 22.99
}
]

$..book[?(@.isbn)]所有具有isbn属性的书$.store.book[?(@.price 所有价格小于10的书$..book[?(@.price 所有价格低于expensive字段的书

$..book[?(@.author =~ /.*REES/i)]

所有符合正则表达式的书
[
{
”category” : “reference”,
”author” : “Nigel Rees”,
”title” : “Sayings of the Century”,
”price” : 8.95
}
]

$..*返回所有$..book.length[

]操作符描述==等于符号,但数字1不等于字符1(note that 1 is not equal to ‘1’)!=不等于符号小于符号小于等于符号大于符号>=大于等于符号=~判断是否符合正则表达式,例如[?(@.name =~ /foo.*?/i)]in所属符号,例如[?(@.size in [‘S’, ‘M’])]nin排除符号sizesize of left (array or string) should match rightempty判空符号

例如:

1)所有具有isbn属性的书

$.store.book[?(@.isbn)].author

AI写代码

2)所有价格大于10的书

$.store.book[?(@.price > 10)]

3)查询xxx==3的所有对象

$.result.list[?(@.xxx ==3)]

jsonpath online evaluator

JSONPath Expression Tester

在这里,你可以将你的json格式的数据拷贝上去,自己手动写表达式解析查看。