fredllaranjo / query-json

Search JSON using regex

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Search JSON using regex


Languages

Language:JavaScript 100.0%