0

I am trying to generate a JSON payload based on a defined structure. I have found various examples of single array objects, but cannot find one that fits a multi array.

Example code (that doesn't work):

package main

import "encoding/json"
import "fmt"

type DiscMessage struct {
    Embeds []struct {
        Title       string `json:"title"`
        Description string `json:"description"`
        URL         string `json:"url"`
        Color       int    `json:"color"`
    } `json:"embeds"`
}


func main() {

var values = DiscMessage{Embeds{{"title1", "description1", "url1", 6545520}}}
encjson, _:= json.Marshal(values)
fmt.Println(string(encjson))
}

Intended output:

{
 "embeds": [{
    "title": "title1",
    "description": "description1",
    "url": "url1",
    "color": 6545520
  }]
}

What is the best approach to this? Eventually I will replace the values with variables, and the possibility of more containers, for example a full discord webhook (https://leovoel.github.io/embed-visualizer/)

5
  • 2
    You need to properly initialize the values: golang.org/ref/spec#Composite_literals. I'd recommend that rather than using a slice of anonymous structs you declare the Embed struct type separately and use that as the element type of the slice field. Commented May 4, 2020 at 6:10
  • @mkopriva I understand that I could create the array with a composite literal, but when I do that I can't seem to attach the value pairs in json. Commented May 4, 2020 at 6:26
  • 2
    For you to able to use Go to produce your desired json, you first need valid Go code, code that will compile, but the code you've shared in your question will not compile, so first follow the suggestion in the first comment, and then, after it compiles and your json is still not what you want you can update the question with the code that compiles and we will be able to help further. Commented May 4, 2020 at 6:33
  • ... and if the suggestion from the first comment isn't clear let me know, specifically. Commented May 4, 2020 at 6:34
  • @mkopriva sorry, I should have specified, the code is broken because I changed Embeds struct to Embeds []struct in an attempt to have an array within an array. That compiles, but when I changed it I am unsure where to go from there. Commented May 4, 2020 at 6:37

1 Answer 1

2

How to initialize a slice of anonymous struct:

type DiscMessage struct {
    Embeds []struct {
        Title       string `json:"title"`
        Description string `json:"description"`
        URL         string `json:"url"`
        Color       int    `json:"color"`
    } `json:"embeds"`
}

_ = DiscMessage{Embeds: []struct{
    Title       string `json:"title"`
    Description string `json:"description"`
    URL         string `json:"url"`
    Color       int    `json:"color"`
}{
    {"title1", "description1", "url1", 6545520},
}}

As you can see this can get too verbose for any sane mind, and if you have to do the initialization in a lot of other places it's going to be a real burden.

To remedy this you can declare the slice's element type, i.e. give it a name so it's not anonymous anymore and save yourself some unnecessary typing.

type DiscMessageEmbed struct {
    Title       string `json:"title"`
    Description string `json:"description"`
    URL         string `json:"url"`
    Color       int    `json:"color"`
}

type DiscMessage struct {
    Embeds []DiscMessageEmbed `json:"embeds"`
}

_ = DiscMessage{Embeds: []DiscMessageEmbed{{"title1", "description1", "url1", 6545520}}}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this makes a lot more sense.

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.