Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

javascript - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...