Nested JSON objects - do I have to use arrays for everything?

JavascriptJqueryJsonSyntaxParsing

Javascript Problem Overview


Is there any way to have nested objects in JSON so I don't have to make arrays out of everything? For my object to be parsed without error I seem to need a structure like this:

{"data":[{"stuff":[
	{"onetype":[
		{"id":1,"name":"John Doe"},
		{"id":2,"name":"Don Joeh"}
	]},
	{"othertype":[
		{"id":2,"company":"ACME"}
	]}]
},{"otherstuff":[
	{"thing":
		[[1,42],[2,2]]
	}]
}]}

If I fetch this object into a variable called "result" I have to access the nested objects like this:

result.data[0].stuff[0].onetype[0]

and

result.data[1].otherstuff[0].thing[0]

This seems clumsy and redundant to me, if possible I would prefer:

result.stuff.onetype[0]

and

result.otherstuff.thing

But how can I use the object keys directly when everything is an array? To my confused and uneducated mind something like this would seem more appropriate:

{"data":
	{"stuff":
		{"onetype":[
			{"id":1,"name": ""},
			{"id":2,"name": ""}
		]}
		{"othertype":[
			{"id":2,"xyz": [-2,0,2],"n":"Crab Nebula","t":0,"c":0,"d":5}
		]}
	}
	{"otherstuff":
		{"thing":
			[[1,42],[2,2]]
		}
	}
}

I've probably misunderstood something fundamental here, but I cannot get the jQuery parser (nor the native FF parser used by jQuery 1.4) to accept the second style object. If anyone can enlighten me it would be gratefully appreciated!

Javascript Solutions


Solution 1 - Javascript

You don't need to use arrays.

JSON values can be arrays, objects, or primitives (numbers or strings).

You can write JSON like this:

{ 
	"stuff": {
		"onetype": [
			{"id":1,"name":"John Doe"},
			{"id":2,"name":"Don Joeh"}
		],
		"othertype": {"id":2,"company":"ACME"}
	}, 
	"otherstuff": {
		"thing": [[1,42],[2,2]]
	 }
}

You can use it like this:

obj.stuff.onetype[0].id
obj.stuff.othertype.id
obj.otherstuff.thing[0][1]  //thing is a nested array or a 2-by-2 matrix.
							//I'm not sure whether you intended to do that.

Solution 2 - Javascript

Every object has to be named inside the parent object:

{ "data": {
	"stuff": {
		"onetype": [
			{ "id": 1, "name": "" },
			{ "id": 2, "name": "" }
		],
		"othertype": [
			{ "id": 2, "xyz": [-2, 0, 2], "n": "Crab Nebula", "t": 0, "c": 0, "d": 5 }
		]
	},
	"otherstuff": {
		"thing":
			[[1, 42], [2, 2]]
	}
  }
}

So you cant declare an object like this:

var obj = {property1, property2};

It has to be

var obj = {property1: 'value', property2: 'value'};

Solution 3 - Javascript

You have too many redundant nested arrays inside your jSON data, but it is possible to retrieve the information. Though like others have said you might want to clean it up.

use each() wrap within another each() until the last array.

for result.data[0].stuff[0].onetype[0] in jQuery you could do the following:

$.each(data.result.data, function(index0, v) {
	$.each(v, function (index1, w) {
		$.each(w, function (index2, x) {
			alert(x.id);
		});
	});

});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn SchulzeView Question on Stackoverflow
Solution 1 - JavascriptSLaksView Answer on Stackoverflow
Solution 2 - JavascriptIgor ZevakaView Answer on Stackoverflow
Solution 3 - JavascriptsyarulView Answer on Stackoverflow