penitent_tangent
penitent_tangent

Reputation: 772

How do I nest a JSON array?

I am trying to create a JSON object. It represents a payload of many observations, all sent from a device with a single serial. Each observation has an ID (8 and 17 in the example below), a dimension and a value. I came up with this:

{
	"serial": "10002000",
	"observations": [
		"8": {
			"d": "dimension1",
			"v": "somevalue",
		},
		"17": {
			"d": "dimension2",
			"v": "anothervalue",
		},
	],
}

Which I think demonstrates what I'm after - but it's not syntactically correct JSON. What am I missing?

Upvotes: 0

Views: 67

Answers (1)

josephmbustamante
josephmbustamante

Reputation: 352

A JSON array can't have key-value pairs as a single element - both the key and a value are separate elements. When you say 8: {...}, you're trying to put a whole key-value pair in the array as one element. You could either change observations to be an object, with 8 as a property, for example, or you could make each element of the array a new object, where 8 is a property within that object, for example.

Ex:

{
    "serial": "10002000",
    "observations": {
        "8": {
            "d": "dimension1",
            "v": "somevalue"
        },
        "17": {
            "d": "dimension2",
            "v": "anothervalue"
        }
    }
}

or

{
    "serial": "10002000",
    "observations": [
        {
            "8": {
                "d": "dimension1",
                "v": "somevalue"
            }
        },
        {
             "17": //and so on

Upvotes: 1

Related Questions