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 implements the ability to query json data structures using SQL select statements. Home page:


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


JSONPath is like an XPath for JSON data structures. Home page:


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


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:


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


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:


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

Linq. Js

Linq. Js -- -- linq in Javascript

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


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

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:

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

Paul's Javascript array filtering method in programming gems , the home page:

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( )", /^con/i);
// using customer table data from SQL Server's northwind database...    
customers.where( "( el, i, res, param ) => == 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: