Unmarshalling json lists with multiple types2. {
"things": [
{
"name": "Alice",
"age": 37
},
{
"city": "Ipoh",
"country": "Malaysia"
},
{
"name": "Bob",
"age": 36
},
{
"city": "Northampton",
"country": "England"
}
]
}
map[string]Person|Place
3. To get us started
type Person struct {
Name string
Age int
}
type Place struct {
City string
Country string
}
func solution(jsonString []byte) ([]Person []Place) {}
Error handling is allowed to be good -> non existent and inconsistent
4. {
"things": [
{
"name": "Alice",
"age": 37
},
{
"city": "Ipoh",
"country": "Malaysia"
},
{
"name": "Bob",
"age": 36
},
{
"city": "Northampton",
"country": "England"
}
]
}
map[string]Person|Place
map[string][]map[string]interface{}
5. Map & type assert
func solutionA(jsonStr []byte) ([]Person, []Place) { func addPerson(persons []Person, item map[string]interface
persons := []Person{} {}) []Person {
places := []Place{} name, _ := item["name"].(string)
var data map[string][]map[string]interface{} age, _ := item["age"].(int)
err := json.Unmarshal(jsonStr, &data) person := Person{name, age}
if err != nil { persons = append(persons, person)
fmt.Println(err) return persons
return persons, places }
}
for i := range data["things"] { func addPlace(places []Place, item map[string]interface{})
[]Place {
item := data["things"][i]
city, _ := item["city"].(string)
if item["name"] != nil {
country, _ := item["city"].(string)
persons = addPerson(persons, item)
place := Place{city, country}
} else {
places = append(places, place)
places = addPlace(places, item)
return places
}
}
}
return persons, places
}
6. {
"things": [
{
"name": "Alice",
"age": 37
},
{
"city": "Ipoh",
"country": "Malaysia"
},
{
"name": "Bob",
"age": 36
},
{
"city": "Northampton",
"country": "England"
}
]
}
map[string]Person|Place
map[string][]Person&Place??
7. Mixed Type Struct
func solutionB(jsonStr []byte) ([]Person, []Place) { type Mixed struct {
persons := []Person{} Name string `json:"name"`
places := []Place{} Age int `json:"age"`
var data map[string][]Mixed City string `json:"city"`
err := json.Unmarshal(jsonStr, &data) Country string `json:"country"`
if err != nil { }
fmt.Println(err)
return persons, places
}
for i := range data["things"] {
item := data["things"][i]
if item.Name != "" {
persons = append(persons, Person{item.Name, item.Age})
} else {
places = append(places, Place{item.City, item.Country})
}
}
return persons, places
}
8. {
"things": [
{
"name": "Alice",
"age": 37
},
{
"city": "Ipoh",
"country": "Malaysia"
},
{
"name": "Bob",
"age": 36
},
{
"city": "Northampton",
"country": "England"
}
]
}
map[string]Person|Place
map[string][]PleaseLeaveAsJson
9. json.RawMessage
func solutionC(jsonStr []byte) ([]Person, []Place) { func addPersonC(thing json.RawMessage, people []Person) []
people := []Person{} Person {
places := []Place{} person := Person{}
var data map[string][]json.RawMessage if err := json.Unmarshal(thing, &person); err != nil {
err := json.Unmarshal(jsonStr, &data) fmt.Println(err)
if err != nil { } else {
fmt.Println(err) if person != *new(Person) {
return people, places people = append(people, person)
} }
for _, thing := range data["things"] { }
people = addPersonC(thing, people) return people
places = addPlaceC(thing, places) }
} func addPlaceC(thing json.RawMessage, places []Place) []Place
{
return people, places
place := Place{}
}
if err := json.Unmarshal(thing, &place); err != nil {
fmt.Println(err)
} else {
if place != *new(Place) {
places = append(places, place)
}
}
return places
}