JSONPath: contains a filter

Hi everyone, I was wondering if anyone knew of a way to use a regular expression or a wildcard (or pehaps '%LIKE%'in SQL), so I could use JSONPath to search in a large JSON dataset.

For example (and yes, I understand, rather than eval( )entering my data in the application):

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

And I would like to be able to view the data as follows:

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

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

At the moment I only found documentation >, <, =and !=relational operations that do not give me this flexibility.

Does anyone know how I can just simply JSONPath find this data (without having to scroll through all the records)?

I do not want to use Dojo JSONQuery, as this will require a different library. However, this allows you to do this, here is an example of them:

[?description~‘*the*’]

Ask me if you would like to clarify the question.

+5
source share
2 answers

nevermind guys found a way to do this simply using ECMA inside JSONPath, although this is not a native selector / statement. Just used:

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

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

If anyone has a better answer, let me know.

+10
source

- Java, JsonPath

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

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

All Articles