I have dummy JSON format as follows:
{ "a":"guig", "b":"khih", "c":[ { "k1":"ert", "k2":"uii" }, { "k1":"ert1" }, { "k1":"ert2", "k2":"uii2" }], "d":{ "e":"yfyuf", "f":[ { "kk1":"ert", "kk2":"uii" }, { "kk1":"ert1", "kk2":"uii1" }, { "kk1":"ert2", "kk2":"uii2" } ] }, "h":78767 }, { "a":"guig", "b":"khih", "c":[ { "k1":"ert" }, { "k1":"ert1", "k2":"uii" }, { "k1":"ert2", "k2":"uii2" }], "d":{ "e":"yfyuf", "f":[ { "kk1":"ert", "kk2":"uii" }, { "kk1":"ert1", "kk2":"uii1" } ] }, "i":78767 }, ..... I want to convert the above JSON to string in following format (Basically csv compatible where first line will be header of it):
a, b, c1_k1, c2_k1, c3_k1, c1_k2, c2_k2, c3_k2, d_e, d_f1_kk1, d_f2_kk1, d_f3_kk1, d_f1_kk2, d_f2_kk2, d_f3_kk2, h, i guig, khih, ert, ert1, ert2, uii, , uii2, yfyuf, ert, ert1, ert2, uii, uii1, , 78767, guig, khih, ert, ert1, ert2, , uii , uii2, yfyuf, ert, ert1, ert2, uii, uii1, , ,78767, field like c1,..Cn comes as much as key duplication are available and wherever values are not present the logic should append extra comma for empty cell. Values of any key can be Array, Object or String
The Failed logic I tried are following
Logic:1
ItrStr=function(k,h) { if(typeof k == "string"){ itsLine+= k+","; itsHdr += h+","; }else if(k && k.length){ ItrArr(k); } else{ ItrObj(k); } }, ItrArr=function(k) { if(k.length){ k.forEach((o)=>{ ItrStr(o,""); }); }else{ ItrObj(k); } }, ItrObj=function(k) { if(k){ let itsKs=Object.keys(k); itsKs.forEach((o)=> { let k1 = k[o]; ItrStr(k1,o); }); }else { itsLine += ","; itsHdr += ","; } }; let ttp=inputJson; itsLine=""; ItrObj(ttp); itsLine=itsLine.replace(/\r/g,"").replace(/\n/g,"")+"\n"; if(!isHdrWrote){ fs.appendFile(dir,itsHdr); isHdrWrote=true; }else{ fs.appendFile(dir,itsLine); } Logic: 2
findObjectByHDR = function(obj) { if(!obj){ return "" } if(Array.isArray(obj)) { obj.forEach((o)=>{ if(o){ findObjectByHDR(o); } }); }else{ for(let i in obj) { if(obj.hasOwnProperty(i)){ if(typeof obj[i] == "string" || typeof obj[i]== "number"){ if(i.length == 1){ console.log("i: ",i," : ",obj); } if(agrtHdr.indexOf(i)<0){ agrtHdr.push(i); }/*else { if(addDups[i]){ addDups[i]++; }else{ addDups[i]=0; } agrtHdr.push(addDups[i]+"---"+i); }*/ }else if(Array.isArray(obj[i])) { obj[i].forEach((o)=>{ if(o){ //console.log("arr",o); findObjectByHDR(o); } }); }else { for(let i1 in obj[i]) { if(obj[i].hasOwnProperty(i1)){ if(typeof obj[i][i1] == "string" || typeof obj[i][i1]== "number"){ if(agrtHdr.indexOf(i1)<0){ agrtHdr.push(i1); } }else{ findObjectByHDR(obj[i][i1]); } } } } } } } }, findObjectByLBL = function(obj,lbl) { if(!obj){ return "" } if(Array.isArray(obj)) { obj.forEach((o)=>{ if(o){ findObjectByLBL(o,lbl); } }); }else if(typeof obj[lbl] == "string" || typeof obj[lbl]== "number"){ return ""+obj[lbl]; } else if(typeof obj != "string" && typeof obj!= "number") { for(let i in obj) { if(obj.hasOwnProperty(i)){ if(Array.isArray(obj[i])) { obj[i].forEach((o)=>{ if(o){ findObjectByLBL(o,lbl); } }); }else if(obj[i]){ if(typeof obj[i] != "string" && typeof obj[i]!= "number"){ findObjectByLBL(obj[i],lbl); } } } } } } let ttp=inputJson; itsHdr=""; var crntLin=[]; if(!dtRed){ findObjectByHDR(ttp); } if(dtRed){ for(let ik=0;ik<agrtHdr.length;ik++){ let iitln=findObjectByLabel(ttp,agrtHdr[ik]); if(!iitln){ console.log(ik,":",lineNr,"--",agrtHdr[ik]," : agrtHdr: "," : ",iitln); } if(iitln){ crntLin.push(iitln.replace(/\r/g,"").replace(/\n/g,"")); }else{ crntLin.push(""); } } fs.appendFile(dir,crntLin.join(",")+"\n"); }
Object.prototype.paths()which will return you all the paths in any object including JSON. There are values many paths so it returns an object where the properties of the returned object are the values of the JSON object and each property will have values in the form of arrays where items designate the nodes on the path. If a value is present at the end of multiple paths (likeertoruii) they will have multiple path arrays. You have to resolve these paths and properties for your case. stackoverflow.com/a/37836123/4543207 Check it with your data repl.it/C6ZV