json-path: Filter on child of an array within another array

Viewed 3420

I have a JSONArray within a JSONArray, I want to apply JSONPath expression on this in such a way that i get JSONObject or JSONArray as a result when a condition is satsified on the inner JSONArray.

Eg:

{
  "A": [
    {
      "B": [
        {
          "id": 1
        },
        {
          "id": 2
        },
        {
          "id": 3
        }
      ],
      "C": {
        "id": 10,
        "name": "PQR"
      },
      "id": 25,
      "name": "XYZ"
    },
    {
      "B": [
        {
          "id": 4
        },
        {
          "id": 5
        },
        {
          "id": 6
        }
      ],
      "C": {
        "id": 15,
        "name": "PQR"
      },
      "id": 20,
      "name": "XYZ"
    }
  ]
}

if i want all elements of A where C.id = 10, I would use: $.A[?(@.C.id == 10)]

Now, What predicate is to be used to obtain all the objects within A, where B.id = 1? Note: B is an array of JSON objects.

1 Answers

I had success with $.A[?(@.B[?(@.id == 1)])]

but only when using Scala's Gatling implementation: http://jsonpath.herokuapp.com/

The Jayway implementation seems to totally ignore the inner filter and according to an issue on their GitHub, that's a bug.

Related