Please can someone help me point out what am doing wrong. I have searched other similar questions like this but non of it seems to help resolve this issue I am trying to decode json using Swift Codable
I have created nested codable struct, but it keeps failing.
The json am trying to decode can be found here - https://pastebin.com/C0QCREEF
This is the codable struct that is failing -
struct OutboundFlightData: Codable {
var data: OutboundFlight
}
struct OutboundFlight: Codable {
var outboundFlights: [OutboundFlightDetails]
}
struct OutboundFlightDetails: Codable {
var amount: String
var fareFamily: Bool
var cabin: String
var fareBasis: String
var duration: String
var itinerary: [OutboundFlightItinerary]
}
struct OutboundFlightItinerary: Codable {
var timeOfDeparture: String
var timeOfArrival: String
var dateOfDeparture: String
var dateOfArrival: String
var departureLocation: String
var departureDetails: FlightItineraryDetails
var arrivalLocation: String
var arrivalDetails: [FlightItineraryDetails]
var departureTerminal: String?
var arrivalTerminal: String?
var operatingCompany: String
var operatingDetails: FlightItineraryOperatingDetails
var marketingCompany: String
var marketingDetails: FlightItineraryOperatingDetails
var flightNumber: String
var electronicTicketing: String
}
struct FlightItineraryOperatingDetails: Codable {
var iata: String
var fullName: String
var countryName: String
var logoSmall: String
var logo: String
enum CodingKeys: String, CodingKey {
case iata
case fullName = "full_name"
case logoSmall = "logo_small"
case logo
case countryName = "country_name"
}
}
struct FlightItineraryDetails: Codable {
var iata: String
var name: String
var cityName: String
var stateName: String
var countryName: String
enum CodingKeys: String, CodingKey {
case iata
case name
case cityName = "city_name"
case stateName = "state_name"
case countryName = "country_name"
}
}
I expect the json to decode successfully. However I keep getting an error -
typeMismatch(
Swift.Array<Any>,
Swift.DecodingError.Context(
codingPath: [
CodingKeys(stringValue: "data", intValue: nil),
CodingKeys(stringValue: "outboundFlights", intValue: nil),
_JSONKey(stringValue: "Index 0", intValue: 0),
CodingKeys(stringValue: "itinerary", intValue: nil),
_JSONKey(stringValue: "Index 0", intValue: 0),
CodingKeys(stringValue: "arrivalDetails", intValue: nil)
],
debugDescription: "Expected to decode Array<Any> but found a dictionary instead.",
underlyingError: nil
)
)
But then when I change arrivalDetails from array. I get this error
typeMismatch(
Swift.Dictionary<Swift.String, Any>,
Swift.DecodingError.Context(
codingPath: [
CodingKeys(stringValue: "data", intValue: nil),
CodingKeys(stringValue: "outboundFlights", intValue: nil),
_JSONKey(stringValue: "Index 6", intValue: 6),
CodingKeys(stringValue: "itinerary", intValue: nil),
_JSONKey(stringValue: "Index 1", intValue: 1),
CodingKeys(stringValue: "arrivalDetails", intValue: nil)
],
debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.",
underlyingError: nil
)
)
json["data"][0]["outboundFlights"][0]["itinerary"][0]["arrivalDetails]is an array but the second one saysjson["data"]["outboundFlights"][6]["itinerary"][1]["arrivalDetails]should be a dictionary. Notice the difference in the key paths. You should inspect the test case you're working with, and see what's going on. - AlexanderOutboundFlightData.datais subscripted by"outboundFlights"directly, which shouldn't be possible because it's an array. - Alexander