Created
January 24, 2020 11:56
-
-
Save palashkulsh/d7678b23998db395d93470693a13abe8 to your computer and use it in GitHub Desktop.
documenting json object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function recurse(o ){ | |
let keys=[]; | |
debugger | |
if(typeof(o)=="number"){ | |
keys.push('(number)'); | |
} else if (typeof(o)=="string"){ | |
try{ | |
JSON.parse(o); | |
keys.push('(jsonstring)'); | |
} catch(e){ | |
keys.push('(string)'); | |
} | |
} else if (typeof(o)=="object"){ | |
if (!o) { | |
// null is also object | |
keys.push(''); | |
} else if (Array.isArray(o)){ | |
let allKeys = []; | |
let allUniqueKeys = {}; | |
o.forEach(function(eachElement){ | |
let recKeys = recurse(eachElement); | |
recKeys.forEach(function (eachRecKey){ | |
allUniqueKeys['[].'+eachRecKey]=1; | |
}); | |
}); | |
keys.push.apply(keys, Object.keys(allUniqueKeys)); | |
} else { | |
Object.keys(o).forEach(function(eachKey){ | |
let recKeys = recurse(o[eachKey]); | |
recKeys.forEach(function(eachRecKey){ | |
if(eachRecKey[0]=='('){ | |
//handling primary data types | |
keys.push(eachKey+' '+eachRecKey); | |
} else if (eachRecKey[0]=='[') { | |
//handling array data type | |
keys.push(eachKey+''+eachRecKey); | |
}else { | |
//handling json object | |
keys.push(eachKey+'.'+eachRecKey); | |
} | |
}); | |
}); | |
} | |
} | |
return keys; | |
} | |
var test = {c:{f:[9,10,11]}} | |
console.log(JSON.stringify(recurse(test),null,4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment