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]
asked Apr 6, 2015 at 19:15
2
  • all of which are different . Hard to know what expected results are without some guidelines Commented Apr 6, 2015 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. Commented Apr 6, 2015 at 19:47

2 Answers 2

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

answered Apr 6, 2015 at 19:19
Sign up to request clarification or add additional context in comments.

Comments

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/

answered Apr 6, 2015 at 19:42

2 Comments

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?
An array is an object, there are several ways to test if an object is an array: stackoverflow.com/questions/4775722/check-if-object-is-array one is to test the presence of a "length" property.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.