I'm working against this JSON, trying to get all the keys and values. My issue is, in some cases there are inner objects with more keys and values in them,
Can this be done ONLY recursively in JavaScript? What am I missing?
{
"@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": "?"
}
Here's the code I'm trying to work with:
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;
}
}
}
};