Decoding JSON array with codable - Swift 5

1.2k views Asked by At

I would like to decode the json data in the following link

https://stats.nba.com/stats/assistleaders?LeagueID=00&PerMode=Totals&PlayerOrTeam=Team&Season=2019-20&SeasonType=Regular+Season

However, the rowSet data is an set of json array in inside an array. How to set up the codable struct to decode this data? I am able to set up the following struct to decode the other data.

import Foundation

struct leagueLeader: Codable {
    
    var resource: String
    var parameters: parameters
    
}

struct parameters: Codable {
    var LeagueID: String
    var PerMode: String
    var StatCategory: String
    var Season: String
    var SeasonType: String
}

struct resultSet: Codable {
    
    var name: String
    var headers: [String]

}
2

There are 2 answers

5
tphduy On

The hard part is dealing with rowSet that supposed to contains multiple data types

{
  "rowSet": [
    [
      1,
      1610612756,
      "PHX",
      "Phoenix Suns",
      1987
    ]
  ]
}

The solution is to declare an enum that each case has an associated value representing a codable data type

enum RowSet: Codable, Equatable {
    case integer(Int)
    case string(String)

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let x = try? container.decode(Int.self) {
            self = .integer(x)
            return
        }
        if let x = try? container.decode(String.self) {
            self = .string(x)
            return
        }
        throw DecodingError.typeMismatch(RowSet.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for RowSet"))
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .integer(let x):
            try container.encode(x)
        case .string(let x):
            try container.encode(x)
        }
    }
}

Full of model:

import Foundation

struct Leader: Codable, Equatable {
    let resource: String?
    let parameters: Parameters?
    let resultSets: [ResultSet]?
}

struct Parameters: Codable, Equatable {
    let leagueId, season, seasonType, perMode: String?
    let playerOrTeam: String?

    enum CodingKeys: String, CodingKey {
        case leagueId = "LeagueID"
        case season = "Season"
        case seasonType = "SeasonType"
        case perMode = "PerMode"
        case playerOrTeam = "PlayerOrTeam"
    }
}

struct ResultSet: Codable, Equatable {
    let name: String?
    let headers: [String]?
    let rowSet: [[RowSet]]?
}

enum RowSet: Codable, Equatable {
    case integer(Int)
    case string(String)

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let x = try? container.decode(Int.self) {
            self = .integer(x)
            return
        }
        if let x = try? container.decode(String.self) {
            self = .string(x)
            return
        }
        throw DecodingError.typeMismatch(RowSet.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for RowSet"))
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .integer(let x):
            try container.encode(x)
        case .string(let x):
            try container.encode(x)
        }
    }
}
0
vadian On

This JSON structure is very unusual. It's a kind of comma separated values (CSV) which cannot be decoded implicitly into a struct.

A solution is to decode the array manually

struct RowSet: Decodable {
    let rank, teamID : Int
    let teamAbbreviation, teamName : String
    let ast : Int
    
    init(from decoder: Decoder) throws {
        var arrayContainer = try decoder.unkeyedContainer()
        guard arrayContainer.count == 5 else { throw DecodingError.dataCorruptedError(in: arrayContainer, debugDescription: "The array must contain 5 items") }
        rank = try arrayContainer.decode(Int.self)
        teamID = try arrayContainer.decode(Int.self)
        teamAbbreviation = try arrayContainer.decode(String.self)
        teamName = try arrayContainer.decode(String.self)
        ast = try arrayContainer.decode(Int.self)
    }
    
}

As mentioned in the comments please conform to the naming convention: structs, classes and enums start with a uppercase letter, variables, properties, functions and enum cases start with lowercase letter.