0

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)
3
  • How is your JSON object coming through on the backend? Is it coming through a request body? Commented Jun 8, 2016 at 13:10
  • Check the edit for the updated code Commented Jun 8, 2016 at 13:14
  • Just add the "[" and "]" to the string you get after marshaling. No need for cleverness here. Commented Jun 8, 2016 at 13:26

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.