Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Unmarshal an inconsistent JSON field that can be a string *or* an array of string?

Tags:

json

go

I am having trouble Unmarshalling some Json I don't have control over. There is one field that 99% of the time is a string but occasionally is an array.

type MyListItem struct {
    Date  string `json:"date"`
    DisplayName       string `json:"display_name"`
}

type MyListings struct {
    CLItems []MyListItem `json:"myitems"`
}

var mylist MyListings
err = json.Unmarshal(jsn, &mylist)
if err != nil {
    fmt.Print("JSON:\n%s\n error:%v\n", string(jsn),err)
    return
}

Json is as follows:

{       
    "date": "30 Apr",
    "display_name": "Mr Smith"
},
{
    "date": "30 Apr",
    "display_name": ["Mr Smith", "Mr Jones"],
}

error: json: cannot unmarshal array into Go struct field MyListItem.display_name of type string

like image 888
John F Avatar asked Apr 30 '18 00:04

John F


2 Answers

As an alternative, this builds off of the answer from @ThunderCat but instead of using json.RawMessage, uses interface{} and a type switch:

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

type MyListItem struct {
    Date           string      `json:"date"`
    DisplayName    string      `json:"-"`
    RawDisplayName interface{} `json:"display_name"`
}

func (li *MyListItem) UnmarshalJSON(data []byte) error {
    type localItem MyListItem
    var loc localItem
    if err := json.Unmarshal(data, &loc); err != nil {
        return err
    }
    *li = MyListItem(loc)
    switch li.RawDisplayName.(type) {
    case string:
        li.DisplayName = li.RawDisplayName.(string)
    case []interface{}:
        vals := li.RawDisplayName.([]interface{})
        if len(vals) > 0 {
            li.DisplayName, _ = vals[0].(string)
            for _, v := range vals[1:] {
                li.DisplayName += "&" + v.(string)
            }
        }
    }
    return nil
}

func test(data string) {
    var li MyListItem
    if err := json.Unmarshal([]byte(data), &li); err != nil {
        log.Fatal(err)
    }
    fmt.Println(li.DisplayName)
}

func main() {
    test(`
{       
    "date": "30 Apr",
    "display_name": "Mr Smith"
}`)

    test(`
{
    "date": "30 Apr",
    "display_name": ["Mr Smith", "Mr Jones"]
}`)

}

playground

like image 24
laz Avatar answered Nov 15 '22 06:11

laz


Use json.RawMessage to capture the varying field.

Use the json "-" name to hide the DisplayName field from decoder. The application will fill this field after the top-level JSON is decoded.

type MyListItem struct {
    Date           string          `json:"date"`
    RawDisplayName json.RawMessage `json:"display_name"`
    DisplayName    []string        `json:"-"`
}

Unmarshal the top-level JSON:

var li MyListItem
if err := json.Unmarshal(data, &li); err != nil {
    // handle error
}

Unmarshal the display name depending on the type of the raw data:

if len(li.RawDisplayName) > 0 {
    switch li.RawDisplayName[0] {
    case '"':
        if err := json.Unmarshal(li.RawDisplayName, &li.DisplayName); err != nil {
            // handle error
        }
    case '[':
        var s []string
        if err := json.Unmarshal(li.RawDisplayName, &s); err != nil {
            // handle error
        }
        // Join arrays with "&" per OP's comment on the question.
        li.DisplayName = strings.Join(s, "&")
    }
}

playground example

Incorporate the above into a for loop to handle MyListings:

var listings MyListings
if err := json.Unmarshal([]byte(data), &listings); err != nil {
    // handle error
}
for i := range listings.CLItems {
    li := &listings.CLItems[i]
    if len(li.RawDisplayName) > 0 {
        switch li.RawDisplayName[0] {
        case '"':
            if err := json.Unmarshal(li.RawDisplayName, &li.DisplayName); err != nil {
                // handle error
            }
        case '[':
            var s []string
            if err := json.Unmarshal(li.RawDisplayName, &s); err != nil {
                // handle error
            }
            li.DisplayName = strings.Join(s, "&")
        }
    }
}

playground example

If there's more than one place in the data model where a value can be a string or []string, it can be helpful to encapsulate the logic in a type. Parse the JSON data in an implementation of the json.Unmarshaler interface.

type multiString string

func (ms *multiString) UnmarshalJSON(data []byte) error {
    if len(data) > 0 {
        switch data[0] {
        case '"':
            var s string
            if err := json.Unmarshal(data, &s); err != nil {
                return err
            }
            *ms = multiString(s)
        case '[':
            var s []string
            if err := json.Unmarshal(data, &s); err != nil {
                return err
            }
            *ms = multiString(strings.Join(s, "&"))
        }
    }
    return nil
}

Use it like this:

type MyListItem struct {
    Date        string      `json:"date"`
    DisplayName multiString `json:"display_name"`
}

type MyListings struct {
    CLItems []MyListItem `json:"myitems"`
}

var listings MyListings
if err := json.Unmarshal([]byte(data), &listings); err != nil {
    log.Fatal(err)
}

Playground Example

Here's the code to get the value as a slice of strings instead of as a single string with values joined by &.

type multiString []string

func (ms *multiString) UnmarshalJSON(data []byte) error {
    if len(data) > 0 {
        switch data[0] {
        case '"':
            var s string
            if err := json.Unmarshal(data, &s); err != nil {
                return err
            }
            *ms = multiString{s}
        case '[':
            if err := json.Unmarshal(data, (*[]string)(ms)); err != nil {
                return err
            }
        }
    }
    return nil
}

Playground example.

like image 158
Bayta Darell Avatar answered Nov 15 '22 06:11

Bayta Darell