jsonjsonpathjayway

JSONPath how to filter out objects that contains lists of empty objects?


I'm trying to use JSONPath (Jayway implementation), to get objects that contains a values array, that contains at least 1 non empty value. This is a simplified data I get from some service:

[
  {
    "feature" : "city",
    "values" : [
      {
        "value" : "cba"
      },
      {},
      {
        "value" : "abc"
      }
    ]
  },
  {
    "feature" : "country",
    "values" : [
      {},
      {}
    ]
  }
]

I've dozens of crazy approaches.

  1. Get the objects with the value attribute
$.[?(@..value)]

but this filters nothing I can still see both objects instead of only one.

  1. I've also tried to filter by the length of the filtered values array but it doesn't work either, and returns an empty list:
$.[?(@.values[?(@.value)].length()>1)]

I'm trying to work it out using online evaluator from jsonpath.herokuapp.com I've been searching a lot and it seems that getting the length of the filtered array isn't possible, but the first approach looks like something that should work. Any ideas?


Solution

  • With Jayway-JSONPath you can use the empty Filter Operator

    Filter the object

    $.[?(@..value empty false)]