I'm currently working with this JSON data, attempting to extract all the keys and values. The challenge I'm facing is that some of these keys contain inner objects with additional key-value pairs.
Is it possible to achieve this recursively using JavaScript exclusively? What am I overlooking?
{
"@xmlns:v6": "urn://oracle.bi.webservices/v6",
"v6:pageID": "?",
"v6:reportID": "?",
"v6:report": {
"v6:reportPath": "?",
"v6:reportXml": "?"
},
"v6:reportViewName": "?",
"v6:reportParams": {
"comment": [
"Zero or more repetitions: ",
"Zero or more repetitions: ",
"Zero or more repetitions: ",
"Zero or more repetitions: ",
"Optional: "
],
"v6:filterExpressions": "?",
"v6:variables": {
"v6:name": "?",
"v6:value": "?"
},
"v6:nameValues": {
"v6:name": "?",
"v6:value": "?"
},
"v6:templateInfos": {
"v6:templateForEach": "?",
"v6:templateIterator": "?",
"comment": "Zero or more repetitions: ",
"v6:instance": {
"v6:instanceName": "?",
"comment": "Zero or more repetitions: ",
"v6:nameValues": {
"v6:name": "?",
"v6:value": "?"
}
}
},
"v6:viewName": "?"
},
"v6:options": {
"v6:enableDelayLoading": "?",
"v6:linkMode": "?"
},
"v6:sessionID": "?"
}
It's worth mentioning the code snippet that I'm currently trying to work through:
function parse(data,child,parent){
var nextRept = false;
if(child){
for(var i = 0; i < tmp.parents.length ; i++){
if(tmp.parents[i].name == parent){
if(!tmp.parents[i].children)
tmp.parents[i].children = [];
var keys = Object.keys(data);
for (var k = 0; k < keys.length; k++) {
var val = data[keys[k]];
if(typeof val === 'object')
{
tmp.parents.push({name: keys[k].replace("v6:","")} ); //adding the parent
parse(val,true,keys[k].replace("v6:","")); // adding children recursively
}
if(val == '?')
{ // handle valid param
var attr = false;
if(keys[k].indexOf('@') == 0){
attr = true;
keys[k] = keys[k].replace("@","");
}
tmp.parents[i].children.push({name: keys[k].replace("v6:","") , value : val , isAttr : attr , isRepet : nextRept});
isRepet = false;
}
}
return;
}
}
return;
}
var keys = Object.keys(data);
for (var i = 0; i < keys.length; i++) {
var val = data[keys[i]];
if(typeof val === 'object')
{
tmp.parents.push({name: keys[i].replace("v6:","")} ); //adding the parent
parse(val,true,keys[i].replace("v6:","")); // adding children recursively
}
else{
if(val.indexOf('Zero or more repetitions') != -1){
nextRept = true;
continue;
}
if(val == '?')
{ // handle valid param
var attr = false;
if(keys[i].indexOf('@') == 0){
attr = "true";
keys[i] = keys[i].replace("@","");
}
else{
attr = false;
}
tmp.parents.push({name: keys[i].replace("v6:","").replace("@","") , value : val , isAttr : attr , isRepet : nextRept});
isRepet = false;
}
}
}
};