最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check an array inside json data is empty using jmespath? - Stack Overflow

programmeradmin5浏览0评论

I have json data as below:

[
  {
    "id": "i_1",
    "name": "abc",
    "address": [
      {
        "city": [
          "city1",
          "city2"
        ]
      },
      {
        "city": [
          "city1",
          "city2"
        ]
      }
    ]
  },
  {
    "id": "i_2",
    "name": "def",
    "address": [
      {
        "city": []
      },
      {
        "city": []
      }
    ]
  }
]

Now, I want only that data where city array is not null. So in the above example the output should be 1st element i.e. with id i_1.

How to filter this json using jmespath library?

I have json data as below:

[
  {
    "id": "i_1",
    "name": "abc",
    "address": [
      {
        "city": [
          "city1",
          "city2"
        ]
      },
      {
        "city": [
          "city1",
          "city2"
        ]
      }
    ]
  },
  {
    "id": "i_2",
    "name": "def",
    "address": [
      {
        "city": []
      },
      {
        "city": []
      }
    ]
  }
]

Now, I want only that data where city array is not null. So in the above example the output should be 1st element i.e. with id i_1.

How to filter this json using jmespath library?

Share Improve this question asked Jan 28, 2019 at 8:28 Monarth SarvaiyaMonarth Sarvaiya 1,1268 silver badges21 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You can do this:

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./ajax/libs/jmespath/0.15.0/jmespath.js"></script>

You could do this in pure javascript using filter and every

const items=[{"id":"i_1","name":"abc","address":[{"city":["city1","city2"]},{"city":["city1","city2"]}]},{"id":"i_2","name":"def","address":[{"city":[]},{"city":[]}]}]

const filtered = items.filter(i => i.address.every(a => a.city && a.city.length > 0))

console.log(filtered)

This returns only if every object inside address has a non-empty city array.

You don't need to use jmespath library use filter and `every from the vanilla JS. It is more efficient.

let jsonTxt = '{"data":[{"id":"i_1","name":"abc","address":[{"city":["city1","city2"]},{"city":["city1","city2"]}]},{"id":"i_2","name":"def","address":[{"city":[]},{"city":[]}]}]}'

let jsonData = JSON.parse(jsonTxt);
let items = jsonData.data;
const result = items.filter(i => i.address.every(a => a.city && a.city.length))
console.log('id: ', result[0].id);
//using jmespath
console.log(jmespath.search({data: items}, "data[*].address[*].city"));
<script src="https://cdnjs.cloudflare./ajax/libs/jmespath/0.15.0/jmespath.js"></script>

发布评论

评论列表(0)

  1. 暂无评论