Skip to content

JSON Query - Dojox/Json/Query converted into normal JavaScript File

License

Notifications You must be signed in to change notification settings

harpreetkhalsagtbit/jsonQuery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsonQuery

JSON Query - Dojox/Json/Query converted into normal JavaScript File

Can be used with in Browser and with node.js

Install

$ npm install json_query

How to Use?

Since the code is same as Dojox/Json/Query. Its usability is also same.

Example:

var jsonQuery = require('../query')();

var data = [{"name":"harpreet","age":25,"subjects":[{"name":"English","class":"8"},{"name":"Hindi","class":"8"},{"name":"Math","class":"8"},{"name":"Science","class":"8"}]},{"name":"kuljeet","age":26,"subjects":[{"name":"English","class":"12"},{"name":"Punjabi","class":"12"},{"name":"Math","class":"12"},{"name":"Science","class":"12"}]}]

jsonQuery.query("$..[?class]", data)

How to Use

Additional features

JSON Query contains some enhancments not found in the original implementation. These include:

  1. Path in results (Currently not supported when running in browser): It is often useful to also recieve the paths to the found items. By setting "pathPropName" when creating the JSON Query object, a property name containing the path will be created in each sub-object of the data.

    NOTE: This operation adds properties to the data

    Example:

    var jsonQuery = require('query');
    var _res = jsonQuery({pathPropName: "__path__"}).query("$..[?year>1975]", data)
    console.log(_res[1].__path__)
    
    // output
    // ['bands']['Dire Straits']['albums']['1']
  2. Caching (Currently not supported when running in browser): This feature will save intermediate processing results to cache which can later be used to greatly reduce query time. Especially useful when the JSON is big (millions of records) and queries are similar.

    Example (enabling caching):

    var jsonQuery = require('query');
    var jq = jsonQuery({cacheData: true}); // Create a JsonQuery object with cache
    
    // First query - updates the cache
    var _res1 = jq.query("$..[?year>2000]", data);
    console.log(_res1);
    
    // Second query - will take signifcantly less time to perform (on large data sets)
    var _res2 = jq.query("$..[?year>1976]", data);
    console.log(_res2);

    Example 2 (caching disabled, works same as original implementation):

    var jsonQuery = require('query');
    var jq = jsonQuery(); // JsonQuery without cache
    
    // First query 
    var _res1 = jq.query("$..[?year>2000]", data);
    console.log(_res1);
    
    // Second query - will take same amount of time as first query
    var _res2 = jq.query("$..[?year>1976]", data);
    console.log(_res2);

    Note: Enabling caching will incur increased the memory usage that will persist as long as the JsonQuery object is alive

Quality Assurance

Since the code is same as Dojox/Json/Query. Its Quality is also same. Because it is in Dojox not in Dojo, it may have some issues.

License and Copyright

The jsonQuery is dual licensed under BSD 3-Clause and AFL same as Dojo Toolkit. For more information on the license please see the License Information. The Dojo Toolkit is Copyright (c) 2005-2018, The JS Foundation. All rights reserved.

About

JSON Query - Dojox/Json/Query converted into normal JavaScript File

Resources

License

Stars

Watchers

Forks

Packages

No packages published