1

Let's say I have this JSON:

{
   "toplevel":  [
       {
           "testval": "accept",
           "number":  1
       },
       {
           "testval": "reject",
           "number": 2
       },
       {
           "testval": "accept",
           "number": 3
       },
       {
           "testval": "reject",
           "number": 4
       }
   ]
}

I would like to get the value of "number" from the last member of "toplevel" which has a "testval" of "accept".

$.toplevel[?(@.testval == 'accept')].number gets me this, as you'd expect:

[
  1,
  3
]

But what I want ultimately is to get that "3" as a result.

Since that seems to produce an array, I was hoping something like this would work:
$.toplevel[?(@.testval == 'accept')][-1].number

or even this:

$.toplevel[?(@.testval == 'accept')].number[-1]

But those don't match anything.

Unfortunately I'm limited to pure JSONPath. I'm entering a JSONPath string into a config file, so there's no ability to process the results, or do multiple JSONPath queries.

Any ideas?

David Ranney
  • 59
  • 2
  • 11
  • You would expect `$.toplevel[?(@.testval == 'accept')][1:2].number` to work because `[x:y]` is an array indexing option but it doesn't. It `$.toplevel[2:3].number` does work because it doesn't have the filter but if you include the filter then it doesn't work. I'm not sure why. – Jerry Jeremiah Jun 26 '23 at 04:16

0 Answers0