Teodor Maxim
Teodor Maxim

Reputation: 552

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.

Upvotes: 1

Views: 1557

Answers (3)

Eklavya
Eklavya

Reputation: 18410

Use json.RawMessage as type. RawMessage simply delays the decoding of part of a message, so we can do it ourselves later.

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

First, decode the response and then using json.Unmarshal decode json.RawMessage

x := &Response{}
_ = json.NewDecoder(resp).Decode(x);
x.Names = DecodeName(x.NamesRaw)

DecodeName used for decoding NameRaw data

func DecodeName(nameRaw json.RawMessage) (data []string) {
    var s string
    if err := json.Unmarshal(nameRaw, &s); err == nil {
        v := []string{s}
        return v
    }
    var sn []string
    if err := json.Unmarshal(nameRaw, &sn); err == nil {
        return sn
    }
    return
}

Upvotes: 0

Eli Bendersky
Eli Bendersky

Reputation: 273366

You'll have to decode this into an interface{} and then use a type assertion to check whether the underlying type is a slice or just a string.

 type Response struct {
     Names interface{} `json:"names"` 
 }

Then after decoding into d, you'd do something like:

    slice, ok := d.Names.([]interface{})
    if ok {
      // it was a slice. use it.
    } else {
      // it wasn't a slice - so expect it to be a string
      // and use that, etc.
    }

Upvotes: 1

mkopriva
mkopriva

Reputation: 38203

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

Upvotes: 4

Related Questions