beginner_
beginner_

Reputation: 7622

JSONPath - Filter expression is not working as expected

Can't get a JSONPath to work.

JSON:

{
  "data": [
    {
      "meta": {
        "definition": {
          "title": "ID",
          "type": "text",
          "key": "657876498"
        }
      },
      "attributes": {
        "id": "8606798",
        "name": "ID",
        "content": {
          "value": "ABC"
        }
      }
    }
  ]
}

Path:

$.data[*].attributes[?(@.name=='ID')]

Which returns no match on jsonpath.com or using jsonpath-ng in python.

What am I fundamentally missing that this filter isn't working?

Note: End goal would be to get name and content.value.

EDIT:

On https://jsonpath.herokuapp.com/ the path actually works. hm...implementation dependent?

Upvotes: 2

Views: 2070

Answers (2)

wp78de
wp78de

Reputation: 18950

Indeed, this seems to be an idiosyncrasy of the jsonpath-ng implementation. It looks like the "filter op can only be used on lists", i.e. an iterable.

As a workaround, we can filter on the data-array and pull the attributes afterward instead:

$.data[?(@.attributes.name="ID")].attributes

This gives the same result as Jayway's JsonPath using your path; however, this approach may not be satisfactory in all situations.

Upvotes: 2

pratap
pratap

Reputation: 628

Please try below paths for name and content.value

$.data[:0].attributes.name
$.data[:0].attributes.content.value

Upvotes: 0

Related Questions