Json Query
Install
npm i @sagold/json-query
and use with
const jsonQuery = require("@sagold/json-query")
At first, query acts like a normal json-pointer where its match is passed to the given callback function:
var query = run; var data = "parent": "child": "id": "child-1" ; ;
But query also supports glob-patterns with *
:
var query = run; var data = "parent": "child": "id": "child-1" "neighbour": "child": "id": "child-2" ; ;
and glob-patterns with **
:
var query = run; var data = "parent": "child": "id": "child-1" "neighbour": "child": "id": "child-2" ; ;
To filter the matched objects an object-query string may be appended on each single step:
var query = run; var data = "parent": "valid": true "child": "id": "child-1" "neighbour": "valid": false "child": "id": "child-2" ; ; // same result with
regular expression must be wrapped with {.*}
:
var query = run; var data = "albert": valid: true "alfred": valid: false "alfons": valid: true ; ;
queryGet
If you only require values or pointers, use queryGet to receive an Array as result:
var queryGet = get; // default: queryGet.VALUES var arrayOfValues = ; // ["#/..", "#/..", ...] var arrayOfJsonPointers = ; // [arguments, arguments], where arguments = 0:value 1:object 2:key 3:jsonPointer var arrayOfAllFourArguments = ;
queryDelete
Multiple items on objects or in arrays may also be delete with query.delete:
var queryDelete = delete; ;
Examples
query(data, "#/**/*", callback);
will iterate over each value of the data objectquery(data, "#/**?valid:true", callback);
will select all objects having its property "valid" set totrue
for further examples refer to the unit tests