3

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;
                }


                }
            }

    };
7
  • 3
    You're missing... the code. What have you tried so far? Commented Apr 27, 2014 at 9:36
  • Or rather than what have you tried, what is it that you are attempting to do? You haven't told us, so how can we tell you how to do it? Commented Apr 27, 2014 at 9:41
  • 1
    i've added a small code example (without recursion solution yet) Commented Apr 27, 2014 at 10:12
  • 1
    im trying to get all this json into array with the same hierarchy, which means , if it has children they will be in sub array, and so on.. Commented Apr 29, 2014 at 7:23
  • 1
    i've also added the full parsing code im currently using , which works half way... if the level gets deep (object which contains array and such) the recursion wont work so good :/ Commented Apr 29, 2014 at 7:31

1 Answer 1

2

I wrote this up pretty quickly but it should work for this situation. (Iteration through objects/arrays/strings) .

var obj = {
    "item":     "value",
    "item2":    ["value1","value2","value3"],
    "item3":    {"item3-1": ["item3-1-1", "item3-1-2", "item3-1-3"], "item3-2": {"morestuff": ["morestuff1", "morestuff2","morestuff3"]}}
}

function parseObject(obj){
    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        var item    = obj[key];
        if(typeof item == 'string'){
            console.log(key + ' -> ' + obj[key]);
        }else{
            console.log(key + ' -> ');
            parseObject(item);
        }
      }
    }
}

parseObject(obj);

Results in:

item -> value item2 -> 0 -> value1 1 -> value2 2 -> value3 item3 -> item3-1 -> 0 -> item3-1-1 1 -> item3-1-2 2 -> item3-1-3 item3-2 -> morestuff -> 0 -> morestuff1 1 -> morestuff2 2 -> morestuff3

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Shai! it gave me the right thinking direction!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.