8 ways to query JSON data structures

Source: Internet
Author: User
Tags javascript array

8 ways to query JSON data structures

Have you ever bothered to "find matches in complex JSON data structures"? There are 8 different ways to do this:

Jsonsql

Jsonsql implements the ability to query JSON data structures using SQL SELECT statements.

Example:

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

Home: http://www.trentrichardson.com/jsonsql/

JSONPath

Jsonpath is like an XPath for a JSON data structure.

Example:

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

Home: http://goessner.net/articles/JsonPath/

Jfunk

Jfunk allows you to retrieve complex JSON or JavaScript objects (which will soon be added to the management function). The Jfunk API is designed almost like the jquery API. It replicates the jquery API directly, except for the DOM-specific APIs.

Example:

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

Home: http://code.google.com/p/jfunk/

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 functionality to implement previous ideas, greatly improving the way you use data in JavaScript.

1 varkelly = friends({id:2}).first();

Home: http://www.taffydb.com/

Linq.js

LINQ in Linq.js--javascript (Translator Note: Concepts in. NET, see HTTP://MSDN.MICROSOFT.COM/ZH-TW/LIBRARY/BB397897)

12345 varqueryResult2 = Enumerable.From(jsonArray)    .Where("$.user.id < 200")    .OrderBy("$.user.screen_name")    .Select("$.user.screen_name + ‘:‘ + $.text")    .ToArray();

Home: http://linqjs.codeplex.com/

Home: http://neue.cc/reference.htm

Objeq

Objeq is a simple library that implements real-time querying of Pojso (Plain-old JavaScript Objects, ordinary JavaScript objects).

12 varres = $objeq(data, "age > 40 && gender == ‘female‘ -> name");    // --> Returns [‘Jessica‘]

Home: Https://github.com/agilosoftware/objeq

It uses the property setters of JavaScript, so it works only on newer browsers.

Json:select ()

Use the class CSS selector to query the JSON.

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

Home: http://jsonselect.org/#tryit

The JavaScript array filtering method in Paul's programming Zhu Ji Nanxiong

123456789101112131415161718192021 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");

Home: http://www.paulfree.com/28/javascript-array-filtering/#more-28

This is my favorite way to query JSON data structures at this time. It's very simple, and according to the author, it's very fast.

The idea behind it is similar to John Resig's JavaScript micro-templating: Using the correct expression to convert a very simple string into a JavaScript function.

Of course, there are more powerful solutions. Paul's implementation of the prototype also lacks a syntax check of the filter expression, but I'm sure you should be able to solve the javscript grammar check yourself.

Finally, you have to decide which one is best for your project.

Link: http://blog.jobbole.com/31166/

8 ways to query JSON data structures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.