Search code examples
gounmarshallingjson-api

json string doesnt get unmarshalled with jsonapi.UnmarshalPayload


I am trying to unmarshall a simple jason string:

type City struct {
    ID          int    `jsonapi:"primary,cities"`
    CountryCode string `jsonapi:"attr,countryCode"`
    Name        string `jsonapi:"attr,name"`
}

func TestGetCityByID(t *testing.T) {
    const mockCity = `{
        "data":{
            "type":"cities",
            "id":"123",
            "attributes":{
                "name":"Berlin",
                "countryCode":"DE"
            }
        }
    }`
    city := new(City)
    err := jsonapi.UnmarshalPayload(strings.NewReader(mockCity), &city)
    log.Info(err) //data is not a jsonapi representation of '**neustargeodata.City' 
    log.Info(city)
}

I cannot se any difference between my json string and the result of marshaling a city object, any idea of what I am doing wrong? Thanks a lot in advance!


Solution

  • Ok, I simply had to change &city into city!