首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >kettle中json input中数据的解析

kettle中json input中数据的解析

作者头像
用户12023652
发布2026-07-29 12:26:10
发布2026-07-29 12:26:10
730
举报

一、对于json数据的解析

简介

  • JSONPath - 是xpath在json的应用。

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

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

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

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

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

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

二、JSONPath语法元素和对应XPath元素的对比

XPath

JSONPath

Description

/

$

表示根元素

.

@

当前元素

/

. or []

子元素

..

n/a

父元素

//

..

递归下降,JSONPath是从E4X借鉴的。

*

*

通配符,表示所有的元素

@

n/a

属性访问字符

[]

[]

子元素操作符

|

[,]

连接操作符在XPath 结果合并其它结点集合。JSONP允许name或者数组索引。

n/a

[start:end:step]

数组分割操作从ES4借鉴。

[]

?()

应用过滤表示式

n/a

()

脚本表达式,使用在脚本引擎下面。

()

n/a

Xpath分组

三、jsonpath使用举例

接口返回:

  1. [{
  2. "id": "PRIMARY",
  3. "name": "小学",
  4. "front_id": "PRIMARY",
  5. "front_name": "小学"
  6. }, {
  7. "id": "JUNIOR",
  8. "name": "初中",
  9. "front_id": "JUNIOR",
  10. "front_name": "初中"
  11. }, {
  12. "id": "HIGH",
  13. "name": "高中",
  14. "front_id": "HIGH",
  15. "front_name": "高中"
  16. }, {
  17. "id": "TECHNICAL",
  18. "name": "中专/技校",
  19. "front_id": "TECHNICAL",
  20. "front_name": "中专/技校"
  21. }, {
  22. "id": "COLLEGE",
  23. "name": "大专",
  24. "front_id": "COLLEGE",
  25. "front_name": "大专"
  26. }, {
  27. "id": "BACHELOR",
  28. "name": "本科",
  29. "front_id": "BACHELOR",
  30. "front_name": "本科"
  31. }, {
  32. "id": "MASTER",
  33. "name": "硕士",
  34. "front_id": "MASTER",
  35. "front_name": "硕士"
  36. }, {
  37. "id": "DOCTOR",
  38. "name": "博士",
  39. "front_id": "DOCTOR",
  40. "front_name": "博士"
  41. }]

JSONPath

结果

$.[*].name

所有学历的name

$.[*].id

所有的id

$.[*]

所有元素

$.[(@.length-2)].name

倒数第二个元素的name

$.[2]

第三个元素

$.[(@.length-1)]

最后一个元素

$.[0,1] $.[:2]

前面的两个元素

$.[?(@.name =~ /.*中/i)]

过滤出所有的name包含“中”的书。

$..book[?(@.price<10)]

过滤出价格低于10的书。

$.[*].length()

所有元素的个数

接口返回:

  1. {
  2. "store": {
  3. "book": [
  4. {
  5. "category": "reference",
  6. "author": "Nigel Rees",
  7. "title": "Sayings of the Century",
  8. "price": 8.95
  9. },
  10. {
  11. "category": "fiction",
  12. "author": "Evelyn Waugh",
  13. "title": "Sword of Honour",
  14. "price": 12.99
  15. },
  16. {
  17. "category": "fiction",
  18. "author": "Herman Melville",
  19. "title": "Moby Dick",
  20. "isbn": "0-553-21311-3",
  21. "price": 8.99
  22. },
  23. {
  24. "category": "fiction",
  25. "author": "J. R. R. Tolkien",
  26. "title": "The Lord of the Rings",
  27. "isbn": "0-395-19395-8",
  28. "price": 22.99
  29. }
  30. ],
  31. "bicycle": {
  32. "color": "red",
  33. "price": 19.95
  34. }
  35. },
  36. "expensive": 10
  37. }

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},{”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}],{”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},{”category” : “fiction”,”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)]

所有价格小于10的书

$..book[?(@.price <= $[‘expensive’])]

所有价格低于expensive字段的书

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

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

$..*

返回所有

$..book.length()

[4]

四、过滤器

操作符

描述

==

等于符号,但数字1不等于字符1(note that 1 is not equal to ‘1’)

!=

不等于符号

<

小于符号

<=

小于等于符号

>

大于符号

>=

大于等于符号

=~

判断是否符合正则表达式,例如[?(@.name =~ /foo.*?/i)]

in

所属符号,例如[?(@.size in [‘S’, ‘M’])]

nin

排除符号

size

size of left (array or string) should match right

empty

判空符号

例如:

1)所有具有isbn属性的书

代码语言:javascript
复制
$.store.book[?(@.isbn)].author

2)所有价格大于10的书

代码语言:javascript
复制
$.store.book[?(@.price > 10)]

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

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

4)可以自定义过滤器来获取想要的任何元素,可以多条件查询

五、在线解析器

http://jsonpath.com/

https://jsonpath.curiousconcept.com/

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-08-12,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 一、对于json数据的解析
  • 简介
  • 二、JSONPath语法元素和对应XPath元素的对比
  • 三、jsonpath使用举例
  • 四、过滤器
  • 五、在线解析器
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档