3

I am developing a web application in javascript (both on the server and client side). I am sending back and forth data as json, and I want to be able to parse it on the other side. The problem is that I have several levels of nested objects inside, so this is where I am stuck. For example, I am sending the following data:

var data = {};
data.title = "My Title";
data.metric = {
   fact : "Malicious code detected",
   technique : "XSS"
};
data.subject = {
   userType : "ADMIN",
   userName : "Jack",
   clientNumber : "000",
   terminal : "192.168.1.1"
};
data.context = {
   environment : {
      session : "00",
      hostname : "mainServer",
      sysType : "production"
   },
   resource : {
      wpt : "DIA",
      pid : "1024"
   }
};

On the other side, when I receive it, I just want to be able to completely loop through this object, and print its contents. I have seen a lot of similar questions on stackoverflow, but none of them have been helpful. Here is what I have done so far:

function display(data) {
    var resp = "";
    var prop = null;
    var dataJSON = JSON.parse(data);

    for (prop in dataJSON) {
        if (patternJSON.hasOwnProperty(prop)) {
            resp += "obj" + "." + prop + " = " + dataJSON[prop] + "\n";
        }
    }
    return resp;
}

But I do not know how to make it automatically go deeper, no matter the number of levels. Can somebody point me to the right direction please? Any help would be greatly appreciated! 10x

Andy
  • 61,948
  • 13
  • 68
  • 95
user2435860
  • 778
  • 3
  • 9
  • 22
  • possible duplicate of [Iterate through Nested JavaScript Objects](http://stackoverflow.com/questions/8085004/iterate-through-nested-javascript-objects) – Andy Jul 31 '14 at 15:46
  • I have already seen this post, and it is not the same case for me, I do not want to look for something specific in my json object. For the moment I just want to print it to the console to make sure it works, but later all these fields will go to some tables in a database. – user2435860 Jul 31 '14 at 15:48
  • That answer shows your how to iterate through a nested object which is what your question is about. Amend the code to suit your additional requirements. – Andy Jul 31 '14 at 15:50
  • Is something like JSON.stringify(obj) alright? It's not formatted, but it will print all of the object's contents out. – Curlystraw Jul 31 '14 at 15:54

1 Answers1

5

Define a print function

function print(obj, prefix) {
  prefix = prefix || 'obj';
  return Object.keys(obj).reduce(function(acc, key){
      var value = obj[key];
      if(typeof value === 'object') { 
          acc.push.apply(acc, print(value, prefix + '.' + key));
      }
      else { 
          acc.push(prefix + '.' + key + ' = ' + value);
      }
      return acc;
  }, []);
}

And use it like this print(data).join('\n').

"obj.title = My Title
obj.metric.fact = Malicious code detected
obj.metric.technique = XSS
obj.subject.userType = ADMIN
obj.subject.userName = Jack
obj.subject.clientNumber = 000
obj.subject.terminal = 192.168.1.1
obj.context.environment.session = 00
obj.context.environment.hostname = mainServer
obj.context.environment.sysType = production
obj.context.resource.wpt = DIA
obj.context.resource.pid = 1024"
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98