Decodable for JSON with two structs under the same tag

35 viewsdecodablejsonswift
0

I have this json:

{ "stuff": [
 {
 "type":"car",
 "object":{
  "a":66,
  "b":66,
  "c":66 }},
 {
 "type":"house",
 "object":{
  "d":66,
  "e":66,
  "f":66 }},
 {
 "type":"car",
 "object":{
  "a":66,
  "b":66,
  "c":66 }}
]}

As you can see for “car” and “house” there are different “object” structs, but both under the tag “object”.

It would be ideal if one ended up with something like

struct StuffItem: Decodable {       
  let type: TheType
  let car: Car
  let house: House
}

Is there some Codable, swifty, way to handle this?