3

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?

2
  • 1
    Wouldn't it work to ensure there is at least a first value element in values? Try: $[*][?(@.values[0].value)] Commented Aug 11, 2021 at 0:35
  • Works as well! Thank you. Commented Aug 9, 2022 at 9:21

1 Answer 1

3

With Jayway-JSONPath you can use the empty Filter Operator

Filter the object

$.[?(@..value empty false)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.