You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

jsoncrawler

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsoncrawler

jsoncrawler.js lets you search complex json data

0.1.68
npmnpm
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

jsoncrawler

APM

jsoncrawler is a simple module that search/replace data inside complex javascript objects

Getting started

Add script tag in your header

<script src="https://broadwayinc.dev/jslib/jsoncrawler/0.1.68/jsoncrawler.js"></script>

Or on node.js or webpack based projects:

npm i jsoncrawler

And in your javascript:

import {jsonCrawler} from 'jsoncrawler';

Basic usage

jsonCrawler(<object | array: object to search>, <number | string | boolean: value to search>, <object: options>)

Searching value

Search and locate value inside complex json object

Example

// Let's find value "DIA" and "Come On Down"

let obj = {
    artist: "DIA",
    tracks: [
        "Paradise",
        {
            hidden: "Come On Down"
        }
    ]
}

let result = jsonCrawler(obj, ["Come On Down", "DIA"]);

/*
result returns:
[
  { path: [], key: 'artist', siblings: [ 'tracks' ], value: 'DIA' },
  {
    // 'path' is the key path to the data location:
    path: [ 'tracks', 1 ],
    
    // 'key' is the key name of the value
    key: 'hidden',
    
    // 'siblings' is the key names that are present on the same level
    siblings: [],
    
    // 'value' is the value you have searched
    value: 'Come On Down'
  }
]
*/

// to get to the searched data:

let ComeOnDown = obj;

result[1].path.map(p => {

    // dive in to the key path
    ComeOnDown = ComeOnDown[p];
    
});

// your value is in the key
ComeOnDown = ComeOnDown[result[1].key];

let DIA = obj;
result[0].path.map(p => {
    DIA = DIA[p];
});
DIA = DIA[result[0].key];


Replacing value

You can replace the value easily

Example

let replace = ['Linux', 'Ubuntu', ['Mint', {mini: ['Lubuntu', 'linux']}]];

// replace 'Lubuntu' with 'Xubuntu' and 'Linux' with 'Linus'
 
jsonCrawler(replace, ['Lubuntu', 'Linux'], {
    replace: ['Xubuntu', 'Linus']
});

console.log(JSON.stringify(replace));
// returns ["Linus","Ubuntu",["Mint",{"mini":["Xubuntu","linux"]}]]

Filtering keys

You can exclude your search/replacement in certain key names

Example

let replace = ['Linux', 'Ubuntu', ['Mint', {mini: ['Lubuntu', 'linux']}]];

// replace 'Lubuntu' with 'Xubuntu' and 'Linux' with 'Linus'
// but exclude data inside keyname 'mini'
 
jsonCrawler(replace, ['Lubuntu', 'Linux'], {
    replace: ['Xubuntu', 'Linus'],
    filter: ['mini']
});

console.log(JSON.stringify(replace));
// returns ["Linus","Ubuntu",["Mint",{"mini":["Lubuntu","linux"]}]]

Keywords

jsoncrawler

FAQs

Package last updated on 18 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts