4

In my application I have used the same keyname to get data everywhere, now in json response that data is same but in one place the keyname is changed so I want to rename the keyname of the array in my json this is what I am getting searched on stack overflow but unable to find any reliable way please guide me any good way to do it

{"status":"success","msg":"deleted","pro_data":[]}

I want JSON with these keys:

{"status":"success","msg":"deleted","Images":[]}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Jhony
  • 187
  • 1
  • 4
  • 16

2 Answers2

6

you can use Codable to create JSON model and in that you can customise you key.

I assume your JSON response ({"status":"success","msg":"deleted","pro_data":[]}) available in Data format.

So, See the following code which are used to create JSON model for your data.

struct WSModel: Codable {
    var status  : String?
    var msg     : Int?
    var Images  : [Any]?

    enum CodingKeys: String, CodingKey {
        case currentPage    = "status"
        case msg            = "msg"
        case Images         = "pro_data"
    }
}

Due to there aren't any data type inside your array I have keep Any type of data.This code is work for when keys in response are "status", "msg", "pro_data".

Try this code and let me know still an issue. I hope this will work for you.

Mayur Karmur
  • 2,119
  • 14
  • 35
0

I assume your issue is that you want to continue to use "Images":[] key, without having to change rest of your code.

In your JSON response that you want to change, you can try something like this.

response["Images"] = response["pro_data"]
Shamas S
  • 7,507
  • 10
  • 46
  • 58