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 - npm Package Compare versions

Comparing version

to
0.2.1

13

jsoncrawler.js

@@ -28,3 +28,3 @@ function jsonCrawler(json, search, option) {

let _dataType = (v) => {
return Array.isArray(v) && v.length ? 'array' : (typeof v) === 'object' && v !== null ? 'object' : 'value';
return Array.isArray(v) && v.length ? 'array' : (typeof v) === 'object' && v !== null && Object.keys(v).length ? 'object' : 'value';
};

@@ -80,3 +80,9 @@

// for nested object. need to add node location
if (!filter.includes(key)) {
if(valueType === 'array' && map.value.length === 0) {
map.dataKey = key;
}
else if(valueType === 'object' && Object.keys(value).length === 0) {
map.dataKey = key;
}
else if (!filter.includes(key)) {
map.node.push(key);

@@ -120,3 +126,2 @@

_jsonCrawler(_map.value, callback, _map);
}

@@ -127,3 +132,3 @@ };

if (search.length === 0) {
if (!filter.includes(m.dataKey) && typeof m.value === 'string' || typeof m.value === 'number' || typeof m.value === 'boolean' || m.value === null || m.value === undefined) {
if (!filter.includes(m.dataKey)) {
let node = JSON.parse(JSON.stringify(m));

@@ -130,0 +135,0 @@ found.push({

{
"name": "jsoncrawler",
"version": "0.2.0",
"version": "0.2.1",
"description": "jsoncrawler.js lets you search complex json data",

@@ -5,0 +5,0 @@ "main": "jsoncrawler.js",

const jsonCrawler = require('./jsoncrawler.js');
// let jsonData = {
// singer: {
// female: ['DIA', 'UNE'],
// male: ['Joji', 'Sankanaction']
// },
// track: [
// {
// title: 'Paradise',
// artist: 'DIA',
// etc: {
// recording: {
// studio: 'ACME Water Tower',
// address: 'LA',
// credit: ['Yakko', 'Wakko', ['Dot', 'Steven Spielberg']]
// }
// }
// },
// {
// title: 'Aussie Boy',
// artist: 'UNE',
// etc: {
// recording: {
// studio: 'Ayers Rock',
// address: 'Australia',
// credit: ['Marceline', 'Finn', 'Jake']
// }
// }
// }
// ]
// };
// console.log(JSON.stringify(jsonData, null, 2));
// console.log('\x1b[33m%s\x1b[0m', 'Scan all value');
// console.log(jsonCrawler(jsonData));
let jsonData = {
singer: {
female: ['DIA', 'UNE'],
male: ['Joji', 'Sankanaction']
},
track: [
{
title: 'Paradise',
artist: 'DIA',
etc: {
recording: {
studio: 'ACME Water Tower',
address: 'LA',
credit: ['Yakko', 'Wakko', ['Dot', 'Steven Spielberg']]
}
}
},
{
title: 'Aussie Boy',
artist: 'UNE',
etc: {
recording: {
studio: 'Ayers Rock',
address: 'Australia',
credit: ['Marceline', 'Finn', 'Jake']
}
}
}
]
};
console.log(JSON.stringify(jsonData, null, 2));
console.log('\x1b[33m%s\x1b[0m', 'Scan all value');
console.log(jsonCrawler(jsonData));
// console.log('\x1b[33m%s\x1b[0m', 'search "Steven Spielberg", and replace it with "Bong Joon Ho"');
// console.log(jsonCrawler(jsonData, "Steven Spielberg", {
// replace: 'Bong Joon Ho'
// }));
// console.log('\x1b[32m%s\x1b[0m', JSON.stringify(jsonData, null, 2));
console.log('\x1b[33m%s\x1b[0m', 'search "Steven Spielberg", and replace it with "Bong Joon Ho"');
console.log(jsonCrawler(jsonData, "Steven Spielberg", {
replace: 'Bong Joon Ho'
}));
console.log('\x1b[32m%s\x1b[0m', JSON.stringify(jsonData, null, 2));
// console.log('\x1b[33m%s\x1b[0m', 'search "Jake" and replace it to a new object {arrangers: [\'PRINCESS BUBBLE GUM\', \'FLAME PRINCESS\']');
// console.log('\x1b[33m%s\x1b[0m', 'search "DIA" and replace it to "Baksa Gimm" but expect the data inside "singer"');
// console.log(jsonCrawler(jsonData, ["Jake", 'DIA'], {
// filter: 'singer',
// replace: [{ arrangers: ['PRINCESS BUBBLE GUM', 'FLAME PRINCESS'] }, 'Baksa Gimm'],
// }));
// console.log('\x1b[32m%s\x1b[0m', JSON.stringify(jsonData, null, 2));
console.log('\x1b[33m%s\x1b[0m', 'search "Jake" and replace it to a new object {arrangers: [\'PRINCESS BUBBLE GUM\', \'FLAME PRINCESS\']');
console.log('\x1b[33m%s\x1b[0m', 'search "DIA" and replace it to "Baksa Gimm" but expect the data inside "singer"');
console.log(jsonCrawler(jsonData, ["Jake", 'DIA'], {
filter: 'singer',
replace: [{ arrangers: ['PRINCESS BUBBLE GUM', 'FLAME PRINCESS'] }, 'Baksa Gimm'],
}));
console.log('\x1b[32m%s\x1b[0m', JSON.stringify(jsonData, null, 2));
// let distro = ['Linux', {
// distro: [['Red Hat Enterprise Linux', 'Fedora', ['Debian', {
// base: 'Ubuntu',
// sub: ['Lubuntu', 'Xubuntu', 'Kubuntu', 'Linux'],
// os: 'Linux'
// }]]]
// }, ['Manjaro', 'Arch Linux', 'Linux']];
let distro = ['Linux', {
distro: [['Red Hat Enterprise Linux', 'Fedora', ['Debian', {
base: 'Ubuntu',
sub: ['Lubuntu', 'Xubuntu', 'Kubuntu', 'Linux'],
os: 'Linux'
}]]]
}, ['Manjaro', 'Arch Linux', 'Linux']];
// console.log(JSON.stringify(distro, null, 2))
// console.log('\x1b[33m%s\x1b[0m', 'search "Linux" and replace it to "Mint" except the data nested in "sub"');
// console.log(jsonCrawler(distro, "Linux", {
// filter: 'sub',
// replace: 'Mint'
// }));
// console.log('\x1b[32m%s\x1b[0m', JSON.stringify(distro, null, 2))
let obj = {
artist: "DIA",
tracks: [
"Paradise",
{
hidden: "Come On Down"
}
]
}
console.log(jsonCrawler(obj));
console.log(JSON.stringify(distro, null, 2))
console.log('\x1b[33m%s\x1b[0m', 'search "Linux" and replace it to "Mint" except the data nested in "sub"');
console.log(jsonCrawler(distro, "Linux", {
filter: 'sub',
replace: 'Mint'
}));
console.log('\x1b[32m%s\x1b[0m', JSON.stringify(distro, null, 2))