Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript JSON parsing

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


                }
            }

    };
like image 629
JAisONE Avatar asked Jul 04 '26 06:07

JAisONE


1 Answers

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

like image 120
Shai Mishali Avatar answered Jul 06 '26 18:07

Shai Mishali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!