0

I need to get data from an specific API which is formatted like:

{
  "dates": {
    "2021-10-01": {....},
    "2021-10-02": {....},
    "2021-10-03": {....},
    "2021-10-04": {....},
    "2021-10-05": {....},
    ......
    ......

and I need to access this data:

{
  "dates": {
    "2021-10-01": {
      "countries": {
        "Spain": {
          "date": "2021-10-01",
          "id": "spain",
          "name": "Spain",
          "name_es": "España",
          "name_it": "Spagna",
          "regions": [
            {
              "date": "2021-10-01",
              "id": "andalucia",
              "name": "Andalucía",
              "name_es": "Andalucía",
              "name_it": "Andalucía",
              "source": "John Hopkins University",

So, I have implemented the next structures to Decode the data using JSONDecoder().decode():

struct AutonomousComunityDataResponse: Decodable {
    let dates : datesData
}

struct datesData: Decodable {
    let date : spain
}

struct spain: Decodable {
    let countries : country2
}

struct country2: Decodable {
    let Spain : regionsSpain
}

struct regionsSpain: Decodable {
    let regions : [regionSpain]
}

struct regionSpain: Decodable {
    let date: String
    let today_new_confirmed: Int
}

I am getting an error when decoding the data because

keyNotFound(CodingKeys(stringValue: "date", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "dates", intValue: nil)],
debugDescription: "No value associated with key CodingKeys(stringValue: \"date\", intValue: nil) (\"date\").", underlyingError: nil))

I don't know how to implement this to get the data. I have thought something like a loop and just adding 1 to a variable like: 2020-10-01+1.

I am stuck in this part.

Álvaro R
  • 1
  • 1
  • Your parsing have failed. But without knowing what your JSON looks like, it's hard to tell what's wrong exactly. – Larme Jan 04 '22 at 16:14
  • @Larme It is know visible – Álvaro R Jan 04 '22 at 16:18
  • 2
    You should use dictionaries instead so instead of `let dates : datesData` it should be `let dates : [String: datesData]` and `let countries : [String: Country]` etc. This also mean you can delete some of the types you have defined. Off-topic but please start the name of your structures with a capital letter for better readability. – Joakim Danielson Jan 04 '22 at 16:21
  • @ÁlvaroR Does this help? -- https://stackoverflow.com/questions/50713638/swift-codable-with-dynamic-keys – Rob Jan 04 '22 at 16:40

1 Answers1

0

Please use this encoder for parsing the data.

    import Foundation

// MARK: - AutonomousComunityDataResponse
struct AutonomousComunityDataResponse: Codable {
    let dates: [String: DateValue]
}

// MARK: - DateValue
struct DateValue: Codable {
    let countries: Countries
}

// MARK: - Countries
struct Countries: Codable {
    let spain: Spain

    enum CodingKeys: String, CodingKey {
        case spain = "Spain"
    }
}

// MARK: - Spain
struct Spain: Codable {
    let date, id, name, nameEs: String
    let nameIt: String
    let regions: [Spain]?
    let source: String?

    enum CodingKeys: String, CodingKey {
        case date, id, name
        case nameEs = "name_es"
        case nameIt = "name_it"
        case regions, source
    }
}

let parsedData = try? newJSONDecoder().decode(AutonomousComunityDataResponse.self, from: jsonData)
Qazi Ammar
  • 953
  • 1
  • 8
  • 23