Find a JSON property name that starts with something using JSON Path

Richard Schneider picture Richard Schneider · Nov 26, 2015 · Viewed 7.5k times · Source

Is it possible to find a property name that matches a regex pattern (or at least starts with) using JSON Path. In XPath I can use name() but I haven't been able to find the JSON Path equivalent.

Basically, I what to find all property names that start with x-. Something like $..x-*.

I'll be interested in using any javascript package that does this. Currently, I'm using JSONPath.

Answer

Richard Schneider picture Richard Schneider · Dec 2, 2015

From my googling this is not possible in standard json path. However, the jsonpath-plus package extends the specification with @path; and makes the following possible

$..[?(@path.includes("[\'x-"))]
$..*[?(@property === 'price' && @ !== 8.95)]

The above assumes you are using ES6.