Have you ever worried about "Searching for Matching content in complicated JSON data structures"? This article introduces eight ways to query json data structures, there are always eight ways to query json data structures that are suitable for your project:
JsonSQL
JsonSQL enables you to query data in a json data structure using SQL select statements. Home: http://www.trentrichardson.com/jsonsql/
Example:
The Code is as follows:
Jsonsql. query ("select * from json. channel. items order by title desc", json );
JSONPath
JSONPath is like an XPath for the json data structure. Home: http://goessner.net/articles/JsonPath/
Example:
The Code is as follows:
JsonPath (books, '$ .. book [(@. length-1)]')
Jfunk
JFunk allows you to retrieve complex JSON or Javascript objects (which will soon be added to the management function. The Design of jFunk API is almost similar to that of jQuery API. It directly copies jQuery APIs, except those for DOM APIs.
Home: http://code.google.com/p/jfunk/
Example:
The Code is as follows:
Jf ("> vegetables> * [color = Orange]", Food). get ();
TaffyDB
Have you ever noticed that the literal value of a Javascript Object looks like a record? If you wrap them in an array, do they look like a database table? TaffyDB is a Javascript library that provides powerful database functions to implement previous ideas, greatly improving the way you use data in Javascript.
Home: http://www.taffydb.com/
Example:
The Code is as follows:
Var kelly = friends ({id: 2}). first ();
Linq. js
Linq. js -- LINQ in Javascript
The Code is as follows:
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 query of POJSO (Plain-Old JavaScript Objects, common Javascript Objects. Home: https://github.com/agilosoftware/objeq
The Code is as follows:
Var res = $ objeq (data, "age> 40 & gender = 'female'-> name ");
// --> Returns ['jessica ']
It uses Javascript property setters, so it can only work on newer browsers)
Json: select ()
Use the class CSS selector to query JSON. Home: http://jsonselect.org/#tryit
The Code is as follows:
. Lang: val ("Bulgarian ")~ . Level
The Javascript Array Filtering Method in Paul's programming Pearl, Home: http://www.paulfree.com/28/javascript-array-filtering/#more-28
The Code is as follows:
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 ");
Currently, this is my favorite method for querying JSON data structures. It is very simple and, according to the author, it is very fast.
The idea behind it is similar to John Resig's JavaScript Micro-Templating: use the correct expression to convert a very simple string into a Javascript function.
Of course, there are more powerful solutions. The prototype implemented by Paul still lacks the syntax check for the filter expression, but I believe you can solve the Javscript syntax check yourself.