from jsonpath import jsonpath
data = \
{
"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
},
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
res = jsonpath(data, "$..book[?(@.price < 10)]")
**我想获得“价格”小于10的数据,这是整个数据,可以理解为删除大于10的数据 **
# my result
[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}]
# Below is the data I want
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
我看了文件,但没有找到相应的方法,希望大家能帮我强>
对于JSONPath查询,这(到目前为止)是不可能直接实现的,因为您无法对复杂的子级对象进行筛选,也无法检索子级与谓词匹配的父节点。下面是关于Github的一个相关问题:https://github.com/json-path/JsonPath/issues/287
例如:
这基本上只是意味着:有一个
store
有一个book
节点,有一个book
有一个子price < 10
。(为了说明,我特意将JSON路径设置得冗长,它可以缩短。)如果满足此条件,它将显示完整的存储及其所有子节点,这就是所选择的。如果不满足这些条件,您将得到一个空列表(因为只有一个存储节点)因此,您可能确实希望研究JSON转换器,例如pyjq或jsonbender
相关问题 更多 >
编程相关推荐