JSONPath :contains filter

六月ゝ 毕业季﹏ 提交于 2019-11-28 00:11:53

问题


Hey all, I was wondering if any knew of a way to use a regular expression or wildcard operator (or pehaps '%LIKE%' in SQL) so I could use JSONPath to do searching within a large set of JSON data.

For instance (and yes, I'm parsing, not eval( )ing my data in the app):

var obj = eval ( '({ "hey": "can you find me?" })' );

And I'd like to be able to look through the data like this:

$.[?(@.hey:contains(find))] // (in jQuery terminology)

where the contents of an argument is part or all of the value in the { "key" : "value" } pairs in my data.

At the moment I have only found documentation on >, <, =, and != relational operators, which don't give me that much flexibility.

Does anyone know a way I can just just JSONPath to find this data (without having to loop through all the entries)?

I don't want to use Dojo's JSONQuery, as this would require another library. However, it lets you do this, here their example:

[?description~‘*the*’]

Ask me if you want more clarification of the question.


回答1:


nevermind, guys, found a way to do it by just using ECMA inside of JSONPath, though this is not a native selector / operator. Simply used:

$.[?(/find/.test(@.hey))]

the RegExp test( ) method (which JSONPath evals behind the scenes).

If anyone has a better answer, though, let me know.




回答2:


Also, may be it will be useful for someone. Link to JSONPath Notation

It works for me (JMeter 4.0)

=~

Match a JavaScript regular expression. For example, [?(@.description =~ /cat.*/i)] matches items whose description starts with cat (case-insensitive).




回答3:


If anyone wants the contains solution in Java then this works with JsonPath

Filter<?> filter = Filter.filter(Criteria.where("hey").regex(Pattern.compile(".*find.*")));
System.out.println(JsonPath.read(json, "$..[?]", filter));

Imports

import com.jayway.jsonpath.Criteria;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;


来源:https://stackoverflow.com/questions/2003252/jsonpath-contains-filter

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