jsongounmarshallingencoding-json-go

How to decode JSON in Go which returns multiple elements as array of type and individual elements as type


I am working with an API that sends JSON data. The problem is that an array of a single element shows up as a single value. For example, consider the following JSON:

{ "names": ["Alice","Bob"] }

The API sends this as an array. But when the names field has a single element, the API sends this:

{ "names": "Alice" }

This is how I would normally decode this of response in Go:

type Response struct {
    Names []string `json:"names"`
}

// later

d := &Response{}
_ = json.NewDecoder(resp).Decode(d) // resp would be a http.Response.Body containing the problematic JSON

Go decodes the first JSON correctly. However, after decoding the second JSON, the object contains an empty array.

I don't have any control over the API, so I have to get around this problem. How could I decode this JSON correctly in Go, so that the Names slice contains a single element? Thank you for your help.


Solution

  • You could implement the json.Unmarshaler interface and have it check the 0th raw byte for [ or " to find out whether it's an array or a string respectivelly:

    type StringSlice []string
    
    func (ss *StringSlice) UnmarshalJSON(data []byte) error {
        if data[0] == '[' {
            return json.Unmarshal(data, (*[]string)(ss))
        } else if data[0] == '"' {
            var s string
            if err := json.Unmarshal(data, &s); err != nil {
                return err
            }
            *ss = append(*ss, s)
        }
        return nil
    }
    

    https://play.golang.com/p/2GEJsS2YOLJ