
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
jsoncrawler
Advanced tools
jsoncrawler is a simple module that search/replace data inside complex javascript objects
Add script tag in your header
<script src="https://cdn.jsdelivr.net/npm/jsoncrawler@latest/jsoncrawler.js"></script>
Or on node.js or webpack based projects:
npm i jsoncrawler
And in your javascript:
import jsonCrawler from 'jsoncrawler';
function jsonCrawler(
object_to_search: {[key:string]: any} | any[],
value_to_search: Array<number | string | boolean>,
options: {
replace: Array<number | string | boolean>, // Value to replace. Must be in the same order as the search array.
filter: Array<number | string> // Key names or index numbers to exclude from search/replacement
}): {
path: Array<number | string>, // Nested key path in order to the parent location
key: string | number, // Key names or index number of the parent value
siblings: Array<number | string>, // Key names or index numbers of the siblings that are present on the same level
value: number | string | boolean // Value you have searched
}[]
You can scan the whole object and get the key path, key name, siblings and value
Example
let obj = {
artist: "DIA",
tracks: [
"Paradise",
{
hidden: "Come On Down"
}
]
}
let result = jsonCrawler(obj);
/*
result returns:
[
{ path: [], key: 'artist', siblings: [ 'tracks' ], value: 'DIA' },
{ path: [ 'tracks' ], key: 0, siblings: [ 1 ], value: 'Paradise' },
{
path: [ 'tracks', 1 ],
key: 'hidden',
siblings: [],
value: 'Come On Down'
}
]
*/
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'
}
]
Note: search results does not come in order.
*/
// to get to the searched data:
let ComeOnDown = obj;
result[1].path.forEach(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.forEach(p => {
DIA = DIA[p];
});
DIA = DIA[result[0].key];
You can replace the value easily
Example
let replace = ['Linux', 'Ubuntu', ['Mint', {mini: ['Lubuntu', 'linux']}]];
// replace 'Lubuntu' with 'Xubuntu' and 'Linux' with 'Linus'
// Make sure the search array and replace array values are in the same order.
jsonCrawler(replace, ['Lubuntu', 'Linux'], {
replace: ['Xubuntu', 'Linus']
});
console.log(replace);
// returns ["Linus","Ubuntu",["Mint",{"mini":["Xubuntu","linux"]}]]
You can exclude your search/replacement in certain key names or index numbers
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(replace);
// returns ["Linus","Ubuntu",["Mint",{"mini":["Lubuntu","linux"]}]]
FAQs
jsoncrawler.js lets you search complex json data
The npm package jsoncrawler receives a total of 2 weekly downloads. As such, jsoncrawler popularity was classified as not popular.
We found that jsoncrawler demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.