-1

I have an app that get some JSON data from server, that looks like this:

"operationsInfo": {
    "bill": {
        "activeProviders": [
              {
                "max": 50,
                "min": 10,
                "name": "name1"
              },
              {
                "max": 50,
                "min": 10,
                "name": "name2"
              }
         ]
    },
    "pin": {
        "activeProviders": [
              {
                "max": 50,
                "min": 10,
                "name": "name3"
              },
              {
                "max": 50,
                "min": 10,
                "name": name4
              }
        ]
    }
}

how can I use swift decodable protocol to deserialized this JSON data? my custom object looks like this:

struct operationList: Decodable {

    let name: String
    let action: String
    let min, max: Int
}

the action value in operationList object must equal to "bill" or "pin". finally I want to get an array of operationList object type when decoding JSON data, for example:

let operationListArray = [operationList1, operationList2, operationList3, operationList4] operationList1.action = "bill", operationList1.max = 50, operationList1.name = "name1" operationList2.action = "bill", operationList2.max = 50, operationList2.name = "name2" operationList3.action = "pin", operationList3.max = 50, operationList3.name = "name3" operationList4.action = "pin", operationList4.max = 50, operationList4.name = "name4"

I already see other answer like this for example: How to decode a nested JSON struct with Swift Decodable protocol? but my problem is how I can put "bill" or "pin" in action value, and in future it is possible new key value like "transfer"(like "pin" or "bill") added to JSON data.

behrad
  • 596
  • 1
  • 5
  • 19
  • It's two steps. First decode the JSON as-is. Then map/filter/reduce/whatever that result into your desired structure. – rmaddy Aug 12 '19 at 17:15
  • can you explain me more...I don't know how to do this – behrad Aug 12 '19 at 17:18
  • Exactly what are you asking about here JSON decoding or mapping of actions? – Joakim Danielson Aug 12 '19 at 17:21
  • 1
    JSON decoding not mapping. – behrad Aug 12 '19 at 17:22
  • 1
    Just paste your json into https://app.quicktype.io and you will get a set of swift structs to work with, then use standard decoding for `Codable` which you can find plenty of examples of here and elsewhere on the net. PS, you need to fix your json first since at least the posted json has an incorrect syntax – Joakim Danielson Aug 12 '19 at 17:25

1 Answers1

1

As @rmaddy mentioned in the comments, you want to do this as two steps.

First, create a structure that matches your JSON format and decode it:

struct Response: Decodable {
    let operationsInfo: [String: Providers]
}

struct Providers: Decodable {
    let activeProviders: [Provider]
}

struct Provider: Decodable {
    let name: String
    let min: Int
    let max: Int
}

let response = try JSONDecoder().decode(Response.self, from: data)

Then, declare a structure that represents the format you want your data to be in and map it:

struct ProviderAction {
    let action: String
    let provider: Provider
}

let actions: [ProviderAction] = response.operationsInfo.map { action, providers in
    providers.activeProviders.map { ProviderAction(action: action, provider: $0) }
}.flatMap { $0 }
dan
  • 9,695
  • 1
  • 42
  • 40