Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONPath :contains filter

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.

like image 333
Dan Beam Avatar asked Jan 05 '10 00:01

Dan Beam


People also ask

What is the output of JsonPath () method?

3.3. JsonPath also has functions that we can use at the end of a path to synthesize that path's output expressions: min(), max(), avg(), stddev() and length(). Finally, we have filters. These are boolean expressions to restrict returned lists of nodes to only those that calling methods need.

What is a JsonPath?

JSONPath is a query language for JSON, similar to XPath for XML. It allows you to select and extract data from a JSON document. You use a JSONPath expression to traverse the path to an element in the JSON structure.

What is JsonPath in Rest assured?

JsonPath is an alternative to using XPath for easily getting values from a Object document. It follows the Groovy dot notation syntax when getting an object from the document. You can regard it as an alternative to XPath for XML.

What is array slice operator in JsonPath?

: operator is the array slice operator, so you can slice collections using the syntax [start:end:step] to return a subcollection of a collection. ( ) operator lets you pass a script expression in the underlying implementation's script language. It's not supported by every implementation of JSONPath, however.


1 Answers

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.

like image 173
Dan Beam Avatar answered Sep 20 '22 00:09

Dan Beam