如何计算 JSON 数组中按 JSONPath 对象属性过滤的对象
How to count JSON Objects in array filtered by attribute of the object by JSONPath
我正在编写一个 JSON路径表达式,该表达式计算数组中的对象,同时按对象的字段之一进行过滤。
输入 JSON 来自 http://jsonpath.herokuapp.com/
{
"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
}
而我提取 "category" 对象的表达式是 "fiction"。
$.store.book[?(@.category == "fiction")]
输出符合预期。
[
{
"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
}
]
到目前为止一切顺利。我想要实现的是计算数组中对象的数量。在这种情况下 3. 我试过这个表达式:
$.store.book[?(@.category == "fiction")].length()
输出不是我预期的。
[
4,
5,
5
]
下面的也不行
$.store[?(@.book[*].category == "fiction")].length()
如何计算数组中元素的数量?如果我使用 jq
它可以写成
[.store.book[] | select (.category == "fiction")] | length
答案已经很晚了,但我最近遇到了这种情况,并根据问题中提供的 JSON 使用下面的 JsonPath 查询设法获取了数组内的对象数量:
$.length($.store.book[?(@.category == "fiction")].length())
我正在编写一个 JSON路径表达式,该表达式计算数组中的对象,同时按对象的字段之一进行过滤。
输入 JSON 来自 http://jsonpath.herokuapp.com/
{
"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
}
而我提取 "category" 对象的表达式是 "fiction"。
$.store.book[?(@.category == "fiction")]
输出符合预期。
[
{
"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
}
]
到目前为止一切顺利。我想要实现的是计算数组中对象的数量。在这种情况下 3. 我试过这个表达式:
$.store.book[?(@.category == "fiction")].length()
输出不是我预期的。
[
4,
5,
5
]
下面的也不行
$.store[?(@.book[*].category == "fiction")].length()
如何计算数组中元素的数量?如果我使用 jq
它可以写成
[.store.book[] | select (.category == "fiction")] | length
答案已经很晚了,但我最近遇到了这种情况,并根据问题中提供的 JSON 使用下面的 JsonPath 查询设法获取了数组内的对象数量:
$.length($.store.book[?(@.category == "fiction")].length())