Elliott Elliott - 5 months ago 26
JSON Question

Store a JSON object in a JSON array golang

I have this JSON Object :

{
"name": "test.counter",
"value": 1.234,
"ts": 1234567890
}


And I would like to store the entire thing in a JSON array so it could look like this:

[
{
"name": "test.counter",
"value": 1.234,
"ts": 1234567890
}
]


How can I do this? I'm kind of new to Golang.
EDIT:
I'm creating the JSON object

output := &Output{
Name: url[0],
Milli: ms,
Epoch: time.Now().Unix(),
}
out1, _ := json.Marshal(output)

Answer

You just crate a slice of Output structs, and you can just Marshal that.

output := Output{
    Name:  url[0],
    Milli: ms,
    Epoch: time.Now().Unix(),
    }

outputs := []Output{output}
out1, _ := json.Marshal(outputs)