JSONPath check descendant value in predicate

In XPath, the following is possible:

XML:

<root>
    <parent>
        <child>
            <descendant status="passed">1</descendant>
            <descendant status="passed">2</descendant>
        </child>
    </parent>
    <parent>
        <child>
            <descendant status="passed">3</descendant>
            <descendant status="failed">4</descendant>
        </child>
    </parent>
</root>

XPath: //parent[.//*[@status='failed']]

Result: /root/parent[2]

(i.e. get any parent node where any descendant has status "failed")


Using Google's JSONPath implementation, the following does not work:

JSON:

{
  parents: [{
      children: [{
          descendants: [{
              name: 1,
              status: "passed"
            }, {
              name: 2,
              status: "passed"
            }
          ]
        }
      ]
    }, {
      children: [{
          descendants: [{
              name: 3,
              status: "passed"
            }, {
              name: 4,
              status: "failed"
            }
          ]
        }
      ]
    }
  ]
}

JSONPath: $.parent[?(@..status=='failed')]

Result: Uncaught SyntaxError: jsonPath: Unexpected token .: _v..status=='failed'

In fact, any occurrence of .. or * in the predicate causes a similar error. e.g.

  1. $.parent[?(@..status)] causes Unexpected token .: _v..status and
  2. $.parent[?(@.*)] causes Unexpected token *: _v.*

I also tried dchester/jsonpath's implementation and get the same problem, so I'm not sure whether or not this kind of behavior is supported by JSONPath. I'm not even sure who, if anyone, is defining the specification for JSONPath.

Question: Is this possible with JSONPath? (i.e. get an array of elements in parents who have any descendants with status == "failed" without explicitly writing out the path)

Answers:

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.