Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 730
Richard Schneider Avatar asked Nov 26 '15 07:11

Richard Schneider


2 Answers

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.

like image 186
Richard Schneider Avatar answered Nov 03 '22 06:11

Richard Schneider


Useful to someone else
You can use regular expression using =~ left matches regular expression. For more in formation visit https://github.com/jayway/JsonPath

[?(@.name =~ /foo.*?/i)]
like image 31
Dipak Prajapati Avatar answered Nov 03 '22 07:11

Dipak Prajapati