Find an object in nested data by property value (using JSONPath)

I have this test data:

[
  {
    id: 1,
    l: 'a',
    sub: [
      ]
  },
  {
    id: 2,
    l: 'b',
    sub: [
      {
        id: 4,
        l: 'd'
      },
      {
        id: 5,
        l: 'e'
      },
      {
        id: 6,
        l: 'f',
        sub: [
          {
            id: 7,
            l: 'g'
          }
        ]
      }
    ]
  },
  {
    id: 3,
    l: 'c',
    sub: []
  }
];

And I'm trying to get the path to an object using id: 7. I tried some JSONPath requests, but I just can't figure out how to make JSONPath iterate over all keys suband search there.

How can I map an object to id: 7?

Here is my test plunkr: http://plnkr.co/edit/RoSeRo0L1B2oH3wC5LdU?p=preview

+4
source share
1 answer

This query should work for what you are doing:

$..[?(@.id==7)]

You need to remove the identifier immediately after $..how you want to select the entire object, not just the identifier. You also lacked square brackets around the query.

:

[
    {
        "id": 7,
        "l": "g"
    }
]

l ( ), . .l :

$..[?(@.id==7)].l

:

[
    "g"
]

, - json : http://www.jsonquerytool.com/sample/jsonpathfilterallbypropertyvalue

+12

All Articles