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

北战南征 提交于 2019-12-01 00:06:54

问题


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.


回答1:


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

$..[?(@path.includes("[\'x-"))]

The above assumes you are using ES6.




回答2:


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)]


来源:https://stackoverflow.com/questions/33933205/find-a-json-property-name-that-starts-with-something-using-json-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!