我有一个如下的json数组:
[{
"id": 1,
"store": {
"book": [{
"category": "reference",
"author": "Nigel Rees"
},
{
"category": "fiction",
"author": "J. K. Rowlin"
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
},
{
"id": 2,
"store": {
"book": [{
"category": "reference",
"author": "Herman Melville"
},
{
"category": "fiction",
"author": "Evelyn Waugh"
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}]
我试图获取图书作者像J. K. Rowlin
这样的整个json对象
{
"id": 1,
"store": {
"book": [{
"category": "reference",
"author": "Nigel Rees"
},
{
"category": "fiction",
"author": "J. K. Rowlin"
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
我正在Json Path Online Validator中尝试类似$..store.book[?(@.author=='J. R. R. Tolkien')]
的操作,并且总是得到这样的内部对象。[
{
"category" : "fiction",
"author" : "J. K. Rowlin"
}
]
当查询满足内部条件时是否可以获得整个对象?
答案 0 :(得分:0)
jq查询语言与JSONPath非常相似,因此您可以考虑使用jq。为了解决您的问题,可以使用以下过滤器:
.[] | select( .store.book[].author == "J. K. Rowlin")
或更健壮:
.[] | select( any(.store.book[]; .author == "J. K. Rowlin") )
假设您的JSON位于名为input.json的文件中,并且您使用的是类似bash的shell,则可以这样调用jq:
jq '.[] | select(any(.store.book[]; .author == "J. K. Rowlin"))' input.json
当然,著名作家是J.K.罗琳。