我有如下的json数据:
[
{
"id": "i_1",
"name": "abc",
"address": [
{
"city": [
"city1",
"city2"
]
},
{
"city": [
"city1",
"city2"
]
}
]
},
{
"id": "i_2",
"name": "def",
"address": [
{
"city": []
},
{
"city": []
}
]
}
]
现在,我只需要
city
数组不是null
的那些数据。因此在上面的示例中,输出应为第一个元素,即ID为i_1
。如何使用jmespath库过滤此
json
? 最佳答案
你可以这样做:
var arr = [
{
"id": "i_1",
"name": "abc",
"address": [
{
"city": [
"city1",
"city2"
]
},
{
"city": [
"city1",
"city2"
]
}
]
},
{
"id": "i_2",
"name": "def",
"address": [
{
"city": []
},
{
"city": []
}
]
}
];
console.log(jmespath.search(arr,"[?not_null(address[].city[])]"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jmespath/0.15.0/jmespath.js"></script>