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.
Answer from SLaks on Stack OverflowYou 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.
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'};
How deeply can a JSON object be nested?
Nested json read as [object Object]
JSON Nested Objects | OutSystems
Is it possible to access nested objects, in the way I have my json formatted?
Videos
There is no theoretical limit to how deep JSON objects can be nested, but there usually is a practical limit based on the decoder being used. For example, PHP's json_decode() has a default limit of 512 levels, though it can be adjusted. Read the documentation for the code using the JSON to determine the max depth.
If your JSON is actually hitting depth limits, you probably need to reconsider how your JSON is structured.
Many JSON parsers and formatters use recursion (i.e., ArduinoJSON and the IBM DastaPower Gateway).
This means that deeply nested JSON objects can be used to attack implementations that use this approach (i.e. see this older jansson issue)).
Originally, the JSON testing suite offered by json.org was testing to make sure parsers would fail at 22 levels of nesting.
This also makes sense where data accessing is concerned. Accessing nested data is less readable (and less maintainable) than multiple storage objects.
So, although there's no official limits to JSON nesting levels, in order to allow your data to be portable and safe, I would not recommend any nesting level above 16.
IMHO, the 16-31 nesting levels should be considered a hazard zone and anything above 32 nesting levels should be considered as a design error.