Search all occurrences on a given JSON.
$ npm install query-json
Import queryJson
module:
const queryJson = require("query-json");
Specify both JSON and query regex:
const json = {
"key_a": {
"color": "red"
},
"key_b": {
"color": "white"
},
"key_c": {
"color": "blue"
}
};
Simple query:
const regex = new RegExp('WHITE', 'i');
const result = queryJson.search(json, regex);
console.log(result);
// [ [ 'key_b', 'color' ] ]
Detailed query:
const regex = new RegExp('COLOR', 'i');
const result = queryJson.search(json, regex, {
details: true
});
console.log(result);
// [ { isKey: true, path: [ 'key_a', 'color' ] },
// { isKey: true, path: [ 'key_b', 'color' ] },
// { isKey: true, path: [ 'key_c', 'color' ] } ]
MIT