0

Is it possible to (and if so how do I) recursively get a list of all objects within an object that contains a property with a given key or a given key in a set of keys using JPath?

Example

For this object searching for the type property:

[
  {
    type: 'text'
  },
  {
    type: 'folder',
    children: 
    [
      {
        type: 'text'
      }
    ]
  },
  {
    name: 'Some object without a type'
  }
]

I would get:

[
  {
    type: 'text'
  },
  {
    type: 'folder'
    children:
    [
      {
        type: 'text'
      }
    ]
  },
  {
    type: 'text'
  }
]
SacredGeometry
  • 863
  • 4
  • 13
  • 24

1 Answers1

0

I found the answer. You can use this query syntax:

$..[?(@.type)]

To explain it:

  1. $ is the root node selector
  2. .. is the recursive query syntax
  3. [...] Subscript operator
  4. ?(...) is the expression/ filter syntax
  5. @.type checks for the existence of the type property on the current node being evaluated

as outlined on

https://goessner.net/articles/JsonPath/

SacredGeometry
  • 863
  • 4
  • 13
  • 24