Learn Courses My Dashboard

How do I parse this json into a list?

Hey y’all, I had a question about how to parse a json dictionary into a list in Swift UI…
My json:

{"pagination":{"limit":100,"offset":0,"count":2,"total":2},"data":[{"flight_date":"2021-07-23","flight_status":"scheduled","departure":{"airport":"Chhatrapati Shivaji International (Sahar International)","timezone":"Asia\/Kolkata","iata":"BOM","icao":"VABB","terminal":"2","gate":"46","delay":16,"scheduled":"2021-07-23T23:20:00+00:00","estimated":"2021-07-23T23:20:00+00:00","actual":null,"estimated_runway":null,"actual_runway":null},"arrival":{"airport":"Newark Liberty International","timezone":"America\/New_York","iata":"EWR","icao":"KEWR","terminal":"B","gate":"BHOL","baggage":"11","delay":null,"scheduled":"2021-07-24T04:55:00+00:00","estimated":"2021-07-24T04:55:00+00:00","actual":null,"estimated_runway":null,"actual_runway":null},"airline":{"name":"United Airlines","iata":"UA","icao":"UAL"},"flight":{"number":"830","iata":"UA830","icao":"UAL830","codeshared":null},"aircraft":null,"live":null},{"flight_date":"2021-07-22","flight_status":"landed","departure":{"airport":"Chhatrapati Shivaji International (Sahar International)","timezone":"Asia\/Kolkata","iata":"BOM","icao":"VABB","terminal":"2","gate":"46","delay":10,"scheduled":"2021-07-22T23:20:00+00:00","estimated":"2021-07-22T23:20:00+00:00","actual":"2021-07-22T23:30:00+00:00","estimated_runway":"2021-07-22T23:30:00+00:00","actual_runway":"2021-07-22T23:30:00+00:00"},"arrival":{"airport":"Newark Liberty International","timezone":"America\/New_York","iata":"EWR","icao":"KEWR","terminal":"B","gate":"55","baggage":"B","delay":3,"scheduled":"2021-07-23T04:55:00+00:00","estimated":"2021-07-23T04:55:00+00:00","actual":"2021-07-23T04:42:00+00:00","estimated_runway":"2021-07-23T04:42:00+00:00","actual_runway":"2021-07-23T04:42:00+00:00"},"airline":{"name":"United Airlines","iata":"UA","icao":"UAL"},"flight":{"number":"830","iata":"UA830","icao":"UAL830","codeshared":null},"aircraft":null,"live":null}]}

Have you created the structs to model the data?

Use this tool to guide your in what your structs should look like to decode the data.
https://app.quicktype.io

@Chris_Parker
Yes I have! It is below:

struct FlightModel: Codable {
    let pagination: Pagination
    let data: [Datum]
}

// MARK: - Datum
struct Datum: Codable {
    let flightDate, flightStatus: String
    let departure, arrival: Arrival
    let airline: Airline
    let flight: Flight
    let aircraft: Aircraft
    let live: Live

    enum CodingKeys: String, CodingKey {
        case flightDate = "flight_date"
        case flightStatus = "flight_status"
        case departure, arrival, airline, flight, aircraft, live
    }
}

// MARK: - Aircraft
struct Aircraft: Codable {
    let registration, iata, icao, icao24: String
}

// MARK: - Airline
struct Airline: Codable {
    let name, iata, icao: String
}

// MARK: - Arrival
struct Arrival: Codable {
    let airport, timezone, iata, icao: String
    let terminal: String
    let gate: String?
    let baggage: JSONNull?
    let delay: Int?
    let scheduled, estimated: Date
    let actual, estimatedRunway, actualRunway: Date?

    enum CodingKeys: String, CodingKey {
        case airport, timezone, iata, icao, terminal, gate, baggage, delay, scheduled, estimated, actual
        case estimatedRunway = "estimated_runway"
        case actualRunway = "actual_runway"
    }
}

// MARK: - Flight
struct Flight: Codable {
    let number, iata, icao: String
    let codeshared: JSONNull?
}

// MARK: - Live
struct Live: Codable {
    let updated: Date
    let latitude, longitude, altitude: Double
    let direction: Int
    let speedHorizontal: Double
    let speedVertical: Int
    let isGround: Bool

    enum CodingKeys: String, CodingKey {
        case updated, latitude, longitude, altitude, direction
        case speedHorizontal = "speed_horizontal"
        case speedVertical = "speed_vertical"
        case isGround = "is_ground"
    }
}

// MARK: - Pagination
struct Pagination: Codable {
    let limit, offset, count, total: Int
}

// MARK: - Encode/decode helpers

class JSONNull: Codable, Hashable {

    public static func == (lhs: JSONNull, rhs: JSONNull) -> Bool {
        return true
    }

    public var hashValue: Int {
        return 0
    }

    public init() {}

    public required init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if !container.decodeNil() {
            throw DecodingError.typeMismatch(JSONNull.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for JSONNull"))
        }
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        try container.encodeNil()
    }
}