0

I want to unmarshal this nested JSON into a struct where medias[x].sendto can either be a string or a string array. How can I accomplish this?

Nested JSON:

{
        "jsonrpc": "2.0",
        "result": [
            {
                "userid": "1",
                "username": "username1",
                "medias": [
                    {
                        "mediaid": "1",
                        "userid": "1",
                        "mediatypeid": "1",
                        "sendto": [
                            "me@me.com"
                        ],
                        "period": "1"
                    },
                    {
                        "mediaid": "2",
                        "userid": "1",
                        "mediatypeid": "2",
                        "sendto": "123456789",
                        "period": "1"
                    }
                ]
            }
        ]
    }

My structs are defined and would work well if there wasn't this Sendto var.

type GetData struct {
        Jsonrpc string   `json:"jsonrpc"`
        Result  []Result `json:"result"`
    }
    type Medias struct {
        Mediaid     string   `json:"mediaid"`
        Userid      string   `json:"userid"`
        Mediatypeid string   `json:"mediatypeid"`
        Sendto      []string `json:"sendto"`
        Period      string   `json:"period"`
    }
    type Result struct {
        Userid   string   `json:"userid"`
        Username string   `json:"username"`
        Medias   []Medias `json:"medias"`
    }

Example: https://go.dev/play/p/SUq5e3sKsGc

Thanks in advance!

nexty5
  • 47
  • 6

0 Answers0