Search code examples
xpathjmespath

Is there a JMESPath equivalent to XPath double slash (//) "any descendant" search?


In XPath, to search for any descendants that match a certain path, say, a elements at any level with a b element as a child, one would use the query //a/b.

E.g., for the following document

<foo>
  <bar>
    <a><b>first</b></a>
  </bar>
  <a><b>second</b></a>
</foo>

using the aforementioned xpath, we would find the elements <b>first</b> and <b>second</b>.

Is there an equivalent for JSON and JMESPath?

E.g.:

{
  "foo": {
    "bar": {
      "a": {"b": "first"}
    },
    "a": {"b": "second"}
  }
}

From the above document, is it possible to retrieve {"b": "first"} and {"b": "second"}?


Solution

  • No. The below query

    foo.a
    

    gives

    {
      "b": "second"
    }
    

    and

    foo.*.a | [0]
    

    gives

    {
      "b": "first"
    }