An overview of 8 ways to query the json data structure

  • 2020-03-30 02:21:32
  • OfStack

Eight ways to query the json data structure:

JsonSQL

JsonSQL implements the ability to query json data structures using SQL select statements. Home page: http://www.trentrichardson.com/jsonsql/

Example:


jsonsql.query("select * from json.channel.items order by title desc",json);

JSONPath

JSONPath is like an XPath for JSON data structures. Home page: http://goessner.net/articles/JsonPath/

Example:


jsonPath( books, '$..book[(@.length-1)]')

jfunk

JFunk allows you to retrieve (and will soon add administration) complex JSON or Javascript objects. The design of the jFunk API is almost similar to the jQuery API. It directly copies jQuery's apis, except for those for DOM.
Home page: http://code.google.com/p/jfunk/

Example:


Jf("> vegetables > *[color=Orange]",Food).get();

TaffyDB

Have you ever noticed that the literal values of Javascript objects look like records? If you wrap them in an array, do they look like a database table? TaffyDB is a Javascript library that provides powerful database capabilities to implement previous ideas, greatly improving the way you use data in Javascript.
Home page: http://www.taffydb.com/

Example:


var kelly = friends({id:2}).first();

Linq. Js

Linq. Js -- -- linq in Javascript


var queryResult2 = Enumerable.From(jsonArray)
    .Where("$.user.id < 200")
    .OrderBy("$.user.screen_name")
    .Select("$.user.screen_name + ':' + $.text")
    .ToArray();

objeq

Objeq is a simple library that implements real-time queries against POJSO (plain-old JavaScript Objects, Plain JavaScript Objects). Home page: https://github.com/agilosoftware/objeq


var res = $objeq(data, "age > 40 && gender == 'female' -> name");
// --> Returns ['Jessica']

It USES Javascript property setters, so it only works on newer browsers.

Json: select ()

Use the class CSS selector to query JSON. Home page: http://jsonselect.org/#tryit


.lang:val("Bulgarian") ~ .level

Paul's Javascript array filtering method in programming gems , the home page: http://www.paulfree.com/28/javascript-array-filtering/#more-28


var a = [1,2,3,4,5,6,7,8,9,10];
 // return everything
a.where( "( ) => true" ) ;
//  --> [1,2,3,4,5,6,7,8,9,10]
// return even numbers
a.where( "( n, i ) => n % 2 == 0" ) ;
//  --> [2,4,6,8,10]
// query first 6 products whose category begins with 'con' using extra param and regular expression
products.where( "( el, i, res, param ) => res.length <= 6 && param.test( el.cat )", /^con/i);
// using customer table data from SQL Server's northwind database...    
customers.where( "( el, i, res, param ) => el.country == param", "USA" );

This is by far my favorite way to query JSON data structures. It's very simple and, according to the authors, very fast.
The idea behind it is similar to John Resig's JavaScript micro-templating: convert a very simple string to a JavaScript function using the correct expression.
Of course, there are more powerful solutions. Paul's prototype still lacks syntax checking for filter expressions, but I'm sure you can handle Javscript syntax checking yourself.


Related articles: