Skip to content

Latest commit

 

History

History
64 lines (46 loc) · 978 Bytes

README.md

File metadata and controls

64 lines (46 loc) · 978 Bytes

Query-JSON

Build Status

Search all occurrences on a given JSON.

$ npm install query-json

API

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' ] } ]

License

MIT