1

I am given a json file that contains a variable amount objects, all of which are different.

Here is an example of my JSON file.

{   
"DNS":  {
            "Server":  MYSERVER01
            "IP":  "XXX.XXX.XXX.XXX"
        },
"TST":  {
            "SubKeyCount":  0,
            "View":  0,
            "Handle":  {
                           "IsInvalid":  false,
                           "IsClosed":  false
                       },
            "ValueCount":  309,
            "Name":  "HKEY_LOCAL_MACHINE\\Software\\TST",
            "ComputerName":  "MYSERVER01",
            "Hive":  -2147483646,
            "Path":  "Software\\TST"
        },
"ServiceNow":  null,
"InstalledSoftware":  [
                          {
                              "Status":  true,
                              "Software":  [
                                               "Symantec NetBackup 7.5",
                                               "Symantec NetBackup Client",

                                           ],
                              "Version":  [
                                              "0000",
                                              "7.5",
                                          ]
                          },
                          {
                              "Status":  true,
                              "Software":  "Symantec Endpoint Protection",
                              "Version":  "12"
                          },
                          {
                              "Status":  true,
                              "Software":  "System Center 2012,
                              "Version":  "7.0"
                          }
                      ],
"AutoDuplex":  {
                   "Description":  "NIC Auto/Auto and Duplexing",
                   "Status":  true
               },

"DefaultAdGroups":  [
                        {
                            "Result":  true,
                            "Group":  "Domain Admins"
                        },
                        {
                            "Result":  true,
                            "Group":  "Test-Team"
                        },
                        {
                            "Result":  true,
                            "Group":  MYSERVER01-ADMINS"
                        }
                    ]

}

This is just a handful of objects that could be in my JSON file.

Currently I am making an ajax call form my jquery to read the file and send my jquery the json as a string. I am then parsing the data into json format.

I am a little confused on how I can display this information neatly using <ul>'s since there are so many arrays nested down deeply within the file.

I also am not sure how to dynamically iterate through each object in the json file. Before I started this, I was only used to seeing 1 object per file. This is a little different though and it is not as easy as specifying:

var jsonQC = jQuery.parseJSON(result); //result from controller
jsonQC[1]
Johnrad
  • 2,637
  • 18
  • 58
  • 98
  • `all of which are different` . Hard to know what expected results are without some guidelines – charlietfl Apr 06 '15 at 19:33
  • That's the thing, I don't know. Any of the objects can contain more/less information than what is displayed in my example. It all depends on when the JSON file is being read. – Johnrad Apr 06 '15 at 19:47

2 Answers2

1

There are some datatable jQuery plugins to display complext JSon data. Here is one example: https://editor.datatables.net/examples/advanced/deepObjects.html

Or, you can use recursive functions as explained here: jQuery JSON looping through nested objects

Community
  • 1
  • 1
renakre
  • 8,001
  • 5
  • 46
  • 99
1

How about using dl/dt/dd?

function makeDom(obj) {
    var $dl = $("<dl/>");
    $.each(obj, function(name, val) {
        $("<dt>").text(name).appendTo($dl);
        var $dd = $("<dd>");
        if(val && typeof val === "object") {
            $dd.append(makeDom(val));
        } else {
            $dd.text(val);
        }
        $dd.appendTo($dl);
    });
    return $dl;
}

var obj = {   
"DNS":  {
            "Server":  "MYSERVER01",
            "IP":  "XXX.XXX.XXX.XXX"
        },
"TST":  {
            "SubKeyCount":  0,
            "View":  0,
            "Handle":  {
                           "IsInvalid":  false,
                           "IsClosed":  false
                       },
            "ValueCount":  309,
            "Name":  "HKEY_LOCAL_MACHINE\\Software\\TST",
            "ComputerName":  "MYSERVER01",
            "Hive":  -2147483646,
            "Path":  "Software\\TST"
        },
"ServiceNow":  null,
"InstalledSoftware":  [
                          {
                              "Status":  true,
                              "Software":  [
                                               "Symantec NetBackup 7.5",
                                               "Symantec NetBackup Client",

                                           ],
                              "Version":  [
                                              "0000",
                                              "7.5",
                                          ]
                          },
                          {
                              "Status":  true,
                              "Software":  "Symantec Endpoint Protection",
                              "Version":  "12"
                          },
                          {
                              "Status":  true,
                              "Software":  "System Center 2012",
                              "Version":  "7.0"
                          }
                      ],
"AutoDuplex":  {
                   "Description":  "NIC Auto/Auto and Duplexing",
                   "Status":  true
               },

"DefaultAdGroups":  [
                        {
                            "Result":  true,
                            "Group":  "Domain Admins"
                        },
                        {
                            "Result":  true,
                            "Group":  "Test-Team"
                        },
                        {
                            "Result":  true,
                            "Group":  "MYSERVER01-ADMINS"
                        }
                    ]
};

$(function() {
    makeDom(obj).appendTo("body");
});

Fiddle here: http://jsfiddle.net/robbyn/0u21ewon/

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
  • This is great, and exactly the logic that I am looking for. The only thing really holding me back is when a JSON object contians multiple arrays. With this method, the header shows a `0` or a `1`, what do you think is the best way of stripping this out? – Johnrad Apr 07 '15 at 14:58
  • An array is an object, there are several ways to test if an object is an array: http://stackoverflow.com/questions/4775722/check-if-object-is-array one is to test the presence of a "length" property. – Maurice Perry Apr 08 '15 at 08:26