I have tried this but it is not working properly. It is just giving regular JS string and not in the JSON format
function convert(obj) {
let ret = "{";
for (let k in obj) {
let v = obj[k];
if (typeof v === "function") {
v = v.toString();
} else if (v instanceof Array) {
v = JSON.stringify(v);
} else if (typeof v === "object") {
v = convert(v);
} else {
v = `"${v}"`;
}
ret += `\n ${k}: ${v},`;
}
ret += "\n}";
return ret;
}
Input:
const input = {
rules3: {
fn1: ()=> {
setTimeout(function abc() {console.log("aaaaaaaa")}, 3000);
}
}
}
Expected output:
I need to JSON.parse the converted String back. So below is an example of expected output
'const input = {
"rules3": {
"fn1": ()=> {
"setTimeout(function abc() {console.log("aaaaaaaa")}, 3000)"
}
}
}'
JSON.stringify()on your object. You don't typically need to stringify functions, just the object data is enough