JsonPath教程

测试路径:http://jsonpath.herokuapp.com

1. 介绍

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

2. 操作符

符号描述
$查询的根节点对象,用于表示一个json数据,可以是数组或对象
@过滤器断言(filter predicate)处理的当前节点对象,类似于java中的this字段
*通配符,可以表示一个名字或数字
..可以理解为递归搜索,Deep scan. Available anywhere a name is required.
.<name>表示一个子节点
[‘<name>’ (, ‘<name>’)]表示一个或多个子节点
[<number> (, <number>)]表示一个或多个数组下标
[start:end]数组片段,区间为[start,end),不包含end
[?(<expression>)]过滤器表达式,表达式结果必须是boolean

3. 函数

可以在JsonPath表达式执行后进行调用,其输入值为表达式的结果。

名称描述输出
min()获取数值类型数组的最小值Double
max()获取数值类型数组的最大值Double
avg()获取数值类型数组的平均值Double
stddev()获取数值类型数组的标准差Double
length()获取数值类型数组的长度Integer

4. 过滤器

过滤器是用于过滤数组的逻辑表达式,一个通常的表达式形如:[?(@.age > 18)],可以通过逻辑表达式&&或||组合多个过滤器表达式,例如[?(@.price < 10 && @.category == ‘fiction’)],字符串必须用单引号或双引号包围,例如[?(@.color == ‘blue’)] or [?(@.color == “blue”)]。

 

操作符描述
==等于符号,但数字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 right
empty判空符号
 

5. 示例

{
    "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
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

 

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
]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JsonPath是一种用于解析JSON文档的语法,类似于XPath对于XML文档的作用。通过使用JsonPath,可以方便地查找节点并获所需的数据。JsonPath提供了一种简洁而强大的方式来处理和操作JSON数据。 具体的JsonPath语法可以参考\[2\]中提供的链接,其中有详细的说明和示例。在使用JsonPath时,可以使用一些特殊的符号和表达式来指定要查找的节点和数据。例如,使用"$"表示根节点,使用"."表示当前节点,使用"\[\]"表示数组索引,使用"*"表示通配符等等。 下面是一个示例代码,展示了如何使用JsonPath来获JSON数据中的作者信息: ```python import jsonpath # 假设data_json是一个包含JSON数据的变量 author = jsonpath.jsonpath(data_json, '$.store.book\[*\].author') print(author) ``` 在这个示例中,`data_json`是一个包含JSON数据的变量,`$.store.book\[*\].author`是一个JsonPath表达式,它表示要获`store`节点下的所有`book`节点的`author`字段的值。通过调用`jsonpath.jsonpath`函数并传入相应的参数,可以获到符合条件的作者信息。 希望以上信息能够帮助到您。如果还有其他问题,请随时提问。 #### 引用[.reference_title] - *1* *3* [详细的jsonpath教程](https://blog.csdn.net/xiaodeng2017/article/details/124905029)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [JsonPath完全介绍及详细使用教程](https://blog.csdn.net/qq_36595013/article/details/109455924)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值