Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generated Deeply Nested JSON
//
// Run this javascript file like so
//
// node generate-nested-json.js "a" 1024 64
// Where:
//
// "a" is the nested property to create
// 1024 is the initial max recursion
// 64 is the amount of times to multiple the initial max recursion.
//
const prop = process.argv[2] || "a";
const max = parseInt(process.argv[3]) || 1024 * 6;
const multiplier = parseInt(process.argv[4]) || 64;
const DEBUG = process.env.DEBUG;
const DELIMITER = "END";
function generateNestedJson(obj, counter) {
if (counter === max) return DELIMITER;
obj[prop] = {};
obj[prop] = generateNestedJson(obj[prop], counter + 1);
return obj;
}
function mutiply(obj) {
let replacer = obj.repeat(1);
for (let i = 0; i < multiplier; i++) {
obj = obj.replace('"' + DELIMITER + '"', replacer);
}
return obj;
}
if (DEBUG) console.log(JSON.stringify(process.argv));
let obj = generateNestedJson({}, 0);
console.log(mutiply(JSON.stringify((obj))));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment