Decoding two different JSON responses in one model class using Codable












0















Based on the requirement I got two different kinds of response from api. That is



{
"shopname":"xxx",
"quantity":4,
"id":1,
"price":200.00,
}


another response



{
"storename":"xxx",
"qty":4,
"id":1,
"amount":200.00,
}


Here both json values are decoding in same model class. Kindly help me to resolve this concern.



is it is possible to set value in single variable like qty and quantity both are stored in same variable based on key param availability










share|improve this question

























  • add both json resposne in one model class like josn { "shopname":"xxx", "quantity":4, "id":1, "price":200.00, "storename":"xxx", "qty":4, "amount":200.00, } and then create model class

    – Vikash Kumar
    Nov 15 '18 at 6:41


















0















Based on the requirement I got two different kinds of response from api. That is



{
"shopname":"xxx",
"quantity":4,
"id":1,
"price":200.00,
}


another response



{
"storename":"xxx",
"qty":4,
"id":1,
"amount":200.00,
}


Here both json values are decoding in same model class. Kindly help me to resolve this concern.



is it is possible to set value in single variable like qty and quantity both are stored in same variable based on key param availability










share|improve this question

























  • add both json resposne in one model class like josn { "shopname":"xxx", "quantity":4, "id":1, "price":200.00, "storename":"xxx", "qty":4, "amount":200.00, } and then create model class

    – Vikash Kumar
    Nov 15 '18 at 6:41
















0












0








0








Based on the requirement I got two different kinds of response from api. That is



{
"shopname":"xxx",
"quantity":4,
"id":1,
"price":200.00,
}


another response



{
"storename":"xxx",
"qty":4,
"id":1,
"amount":200.00,
}


Here both json values are decoding in same model class. Kindly help me to resolve this concern.



is it is possible to set value in single variable like qty and quantity both are stored in same variable based on key param availability










share|improve this question
















Based on the requirement I got two different kinds of response from api. That is



{
"shopname":"xxx",
"quantity":4,
"id":1,
"price":200.00,
}


another response



{
"storename":"xxx",
"qty":4,
"id":1,
"amount":200.00,
}


Here both json values are decoding in same model class. Kindly help me to resolve this concern.



is it is possible to set value in single variable like qty and quantity both are stored in same variable based on key param availability







json swift decodable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 7:28







Parthee Vs

















asked Nov 15 '18 at 6:36









Parthee VsParthee Vs

11




11













  • add both json resposne in one model class like josn { "shopname":"xxx", "quantity":4, "id":1, "price":200.00, "storename":"xxx", "qty":4, "amount":200.00, } and then create model class

    – Vikash Kumar
    Nov 15 '18 at 6:41





















  • add both json resposne in one model class like josn { "shopname":"xxx", "quantity":4, "id":1, "price":200.00, "storename":"xxx", "qty":4, "amount":200.00, } and then create model class

    – Vikash Kumar
    Nov 15 '18 at 6:41



















add both json resposne in one model class like josn { "shopname":"xxx", "quantity":4, "id":1, "price":200.00, "storename":"xxx", "qty":4, "amount":200.00, } and then create model class

– Vikash Kumar
Nov 15 '18 at 6:41







add both json resposne in one model class like josn { "shopname":"xxx", "quantity":4, "id":1, "price":200.00, "storename":"xxx", "qty":4, "amount":200.00, } and then create model class

– Vikash Kumar
Nov 15 '18 at 6:41














2 Answers
2






active

oldest

votes


















1














Here's an approach that lets you have only one property in your code, instead of two Optionals:



Define a struct that contains all the properties you need, with the names that you'd like to use in your code. Then, define two CodingKey enums that map those properties to the two different JSON formats and implement a custom initializer:



let json1 = """
{
"shopname":"xxx",
"quantity":4,
"id":1,
"price":200.00,
}
""".data(using: .utf8)!

let json2 = """
{
"storename":"xxx",
"qty":4,
"id":1,
"amount":200.00,
}
""".data(using: .utf8)!

struct DecodingError: Error {}

struct Model: Decodable {
let storename: String
let quantity: Int
let id: Int
let price: Double

enum CodingKeys1: String, CodingKey {
case storename = "shopname"
case quantity
case id
case price
}

enum CodingKeys2: String, CodingKey {
case storename
case quantity = "qty"
case id
case price = "amount"
}

init(from decoder: Decoder) throws {
let container1 = try decoder.container(keyedBy: CodingKeys1.self)
let container2 = try decoder.container(keyedBy: CodingKeys2.self)

if let storename = try container1.decodeIfPresent(String.self, forKey: CodingKeys1.storename) {
self.storename = storename
self.quantity = try container1.decode(Int.self, forKey: CodingKeys1.quantity)
self.id = try container1.decode(Int.self, forKey: CodingKeys1.id)
self.price = try container1.decode(Double.self, forKey: CodingKeys1.price)
} else if let storename = try container2.decodeIfPresent(String.self, forKey: CodingKeys2.storename) {
self.storename = storename
self.quantity = try container2.decode(Int.self, forKey: CodingKeys2.quantity)
self.id = try container2.decode(Int.self, forKey: CodingKeys2.id)
self.price = try container2.decode(Double.self, forKey: CodingKeys2.price)
} else {
throw DecodingError()
}
}
}


do {
let j1 = try JSONDecoder().decode(Model.self, from: json1)
print(j1)
let j2 = try JSONDecoder().decode(Model.self, from: json2)
print(j2)
} catch {
print(error)
}





share|improve this answer


























  • You shouldn't use Data as your model name. It's already a foundation type.

    – Desdenova
    Nov 15 '18 at 7:31






  • 1





    True, that can be confusing. Updated my answer.

    – Gereon
    Nov 15 '18 at 7:34











  • let json1 = """ { "shopname":"xxx", "quantity":4, "id":1, "price":200.00, } """.data(using: .utf8)! let json2 = """ { "storename":"xxx", "qty":4, "id":1, "amount":200.00, } """.data(using: .utf8)! Here need to pass my json response?? Kindly share some helpful url to know more about my concern.

    – Parthee Vs
    Nov 15 '18 at 7:46













  • That's just an example so you can try the code e.g. in a playground. In your code, you would use your JSON response data in place of my json1 or json2

    – Gereon
    Nov 15 '18 at 8:25













  • @Gereon Thanks You

    – Parthee Vs
    Nov 20 '18 at 13:32



















0














Use like



struct modelClass : Codable {

let amount : Float?
let id : Int?
let price : Float?
let qty : Int?
let quantity : Int?
let shopname : String?
let storename : String?


enum CodingKeys: String, CodingKey {
case amount = "amount"
case id = "id"
case price = "price"
case qty = "qty"
case quantity = "quantity"
case shopname = "shopname"
case storename = "storename"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
amount = try values.decodeIfPresent(Float.self, forKey: .amount)
id = try values.decodeIfPresent(Int.self, forKey: .id)
price = try values.decodeIfPresent(Float.self, forKey: .price)
qty = try values.decodeIfPresent(Int.self, forKey: .qty)
quantity = try values.decodeIfPresent(Int.self, forKey: .quantity)
shopname = try values.decodeIfPresent(String.self, forKey: .shopname)
storename = try values.decodeIfPresent(String.self, forKey: .storename)
}


}





share|improve this answer
























  • Thanks @vikash.. is it is possible to set value in single variable. like qty and quantity both are stored in same variable based on availability

    – Parthee Vs
    Nov 15 '18 at 7:11













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313725%2fdecoding-two-different-json-responses-in-one-model-class-using-codable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Here's an approach that lets you have only one property in your code, instead of two Optionals:



Define a struct that contains all the properties you need, with the names that you'd like to use in your code. Then, define two CodingKey enums that map those properties to the two different JSON formats and implement a custom initializer:



let json1 = """
{
"shopname":"xxx",
"quantity":4,
"id":1,
"price":200.00,
}
""".data(using: .utf8)!

let json2 = """
{
"storename":"xxx",
"qty":4,
"id":1,
"amount":200.00,
}
""".data(using: .utf8)!

struct DecodingError: Error {}

struct Model: Decodable {
let storename: String
let quantity: Int
let id: Int
let price: Double

enum CodingKeys1: String, CodingKey {
case storename = "shopname"
case quantity
case id
case price
}

enum CodingKeys2: String, CodingKey {
case storename
case quantity = "qty"
case id
case price = "amount"
}

init(from decoder: Decoder) throws {
let container1 = try decoder.container(keyedBy: CodingKeys1.self)
let container2 = try decoder.container(keyedBy: CodingKeys2.self)

if let storename = try container1.decodeIfPresent(String.self, forKey: CodingKeys1.storename) {
self.storename = storename
self.quantity = try container1.decode(Int.self, forKey: CodingKeys1.quantity)
self.id = try container1.decode(Int.self, forKey: CodingKeys1.id)
self.price = try container1.decode(Double.self, forKey: CodingKeys1.price)
} else if let storename = try container2.decodeIfPresent(String.self, forKey: CodingKeys2.storename) {
self.storename = storename
self.quantity = try container2.decode(Int.self, forKey: CodingKeys2.quantity)
self.id = try container2.decode(Int.self, forKey: CodingKeys2.id)
self.price = try container2.decode(Double.self, forKey: CodingKeys2.price)
} else {
throw DecodingError()
}
}
}


do {
let j1 = try JSONDecoder().decode(Model.self, from: json1)
print(j1)
let j2 = try JSONDecoder().decode(Model.self, from: json2)
print(j2)
} catch {
print(error)
}





share|improve this answer


























  • You shouldn't use Data as your model name. It's already a foundation type.

    – Desdenova
    Nov 15 '18 at 7:31






  • 1





    True, that can be confusing. Updated my answer.

    – Gereon
    Nov 15 '18 at 7:34











  • let json1 = """ { "shopname":"xxx", "quantity":4, "id":1, "price":200.00, } """.data(using: .utf8)! let json2 = """ { "storename":"xxx", "qty":4, "id":1, "amount":200.00, } """.data(using: .utf8)! Here need to pass my json response?? Kindly share some helpful url to know more about my concern.

    – Parthee Vs
    Nov 15 '18 at 7:46













  • That's just an example so you can try the code e.g. in a playground. In your code, you would use your JSON response data in place of my json1 or json2

    – Gereon
    Nov 15 '18 at 8:25













  • @Gereon Thanks You

    – Parthee Vs
    Nov 20 '18 at 13:32
















1














Here's an approach that lets you have only one property in your code, instead of two Optionals:



Define a struct that contains all the properties you need, with the names that you'd like to use in your code. Then, define two CodingKey enums that map those properties to the two different JSON formats and implement a custom initializer:



let json1 = """
{
"shopname":"xxx",
"quantity":4,
"id":1,
"price":200.00,
}
""".data(using: .utf8)!

let json2 = """
{
"storename":"xxx",
"qty":4,
"id":1,
"amount":200.00,
}
""".data(using: .utf8)!

struct DecodingError: Error {}

struct Model: Decodable {
let storename: String
let quantity: Int
let id: Int
let price: Double

enum CodingKeys1: String, CodingKey {
case storename = "shopname"
case quantity
case id
case price
}

enum CodingKeys2: String, CodingKey {
case storename
case quantity = "qty"
case id
case price = "amount"
}

init(from decoder: Decoder) throws {
let container1 = try decoder.container(keyedBy: CodingKeys1.self)
let container2 = try decoder.container(keyedBy: CodingKeys2.self)

if let storename = try container1.decodeIfPresent(String.self, forKey: CodingKeys1.storename) {
self.storename = storename
self.quantity = try container1.decode(Int.self, forKey: CodingKeys1.quantity)
self.id = try container1.decode(Int.self, forKey: CodingKeys1.id)
self.price = try container1.decode(Double.self, forKey: CodingKeys1.price)
} else if let storename = try container2.decodeIfPresent(String.self, forKey: CodingKeys2.storename) {
self.storename = storename
self.quantity = try container2.decode(Int.self, forKey: CodingKeys2.quantity)
self.id = try container2.decode(Int.self, forKey: CodingKeys2.id)
self.price = try container2.decode(Double.self, forKey: CodingKeys2.price)
} else {
throw DecodingError()
}
}
}


do {
let j1 = try JSONDecoder().decode(Model.self, from: json1)
print(j1)
let j2 = try JSONDecoder().decode(Model.self, from: json2)
print(j2)
} catch {
print(error)
}





share|improve this answer


























  • You shouldn't use Data as your model name. It's already a foundation type.

    – Desdenova
    Nov 15 '18 at 7:31






  • 1





    True, that can be confusing. Updated my answer.

    – Gereon
    Nov 15 '18 at 7:34











  • let json1 = """ { "shopname":"xxx", "quantity":4, "id":1, "price":200.00, } """.data(using: .utf8)! let json2 = """ { "storename":"xxx", "qty":4, "id":1, "amount":200.00, } """.data(using: .utf8)! Here need to pass my json response?? Kindly share some helpful url to know more about my concern.

    – Parthee Vs
    Nov 15 '18 at 7:46













  • That's just an example so you can try the code e.g. in a playground. In your code, you would use your JSON response data in place of my json1 or json2

    – Gereon
    Nov 15 '18 at 8:25













  • @Gereon Thanks You

    – Parthee Vs
    Nov 20 '18 at 13:32














1












1








1







Here's an approach that lets you have only one property in your code, instead of two Optionals:



Define a struct that contains all the properties you need, with the names that you'd like to use in your code. Then, define two CodingKey enums that map those properties to the two different JSON formats and implement a custom initializer:



let json1 = """
{
"shopname":"xxx",
"quantity":4,
"id":1,
"price":200.00,
}
""".data(using: .utf8)!

let json2 = """
{
"storename":"xxx",
"qty":4,
"id":1,
"amount":200.00,
}
""".data(using: .utf8)!

struct DecodingError: Error {}

struct Model: Decodable {
let storename: String
let quantity: Int
let id: Int
let price: Double

enum CodingKeys1: String, CodingKey {
case storename = "shopname"
case quantity
case id
case price
}

enum CodingKeys2: String, CodingKey {
case storename
case quantity = "qty"
case id
case price = "amount"
}

init(from decoder: Decoder) throws {
let container1 = try decoder.container(keyedBy: CodingKeys1.self)
let container2 = try decoder.container(keyedBy: CodingKeys2.self)

if let storename = try container1.decodeIfPresent(String.self, forKey: CodingKeys1.storename) {
self.storename = storename
self.quantity = try container1.decode(Int.self, forKey: CodingKeys1.quantity)
self.id = try container1.decode(Int.self, forKey: CodingKeys1.id)
self.price = try container1.decode(Double.self, forKey: CodingKeys1.price)
} else if let storename = try container2.decodeIfPresent(String.self, forKey: CodingKeys2.storename) {
self.storename = storename
self.quantity = try container2.decode(Int.self, forKey: CodingKeys2.quantity)
self.id = try container2.decode(Int.self, forKey: CodingKeys2.id)
self.price = try container2.decode(Double.self, forKey: CodingKeys2.price)
} else {
throw DecodingError()
}
}
}


do {
let j1 = try JSONDecoder().decode(Model.self, from: json1)
print(j1)
let j2 = try JSONDecoder().decode(Model.self, from: json2)
print(j2)
} catch {
print(error)
}





share|improve this answer















Here's an approach that lets you have only one property in your code, instead of two Optionals:



Define a struct that contains all the properties you need, with the names that you'd like to use in your code. Then, define two CodingKey enums that map those properties to the two different JSON formats and implement a custom initializer:



let json1 = """
{
"shopname":"xxx",
"quantity":4,
"id":1,
"price":200.00,
}
""".data(using: .utf8)!

let json2 = """
{
"storename":"xxx",
"qty":4,
"id":1,
"amount":200.00,
}
""".data(using: .utf8)!

struct DecodingError: Error {}

struct Model: Decodable {
let storename: String
let quantity: Int
let id: Int
let price: Double

enum CodingKeys1: String, CodingKey {
case storename = "shopname"
case quantity
case id
case price
}

enum CodingKeys2: String, CodingKey {
case storename
case quantity = "qty"
case id
case price = "amount"
}

init(from decoder: Decoder) throws {
let container1 = try decoder.container(keyedBy: CodingKeys1.self)
let container2 = try decoder.container(keyedBy: CodingKeys2.self)

if let storename = try container1.decodeIfPresent(String.self, forKey: CodingKeys1.storename) {
self.storename = storename
self.quantity = try container1.decode(Int.self, forKey: CodingKeys1.quantity)
self.id = try container1.decode(Int.self, forKey: CodingKeys1.id)
self.price = try container1.decode(Double.self, forKey: CodingKeys1.price)
} else if let storename = try container2.decodeIfPresent(String.self, forKey: CodingKeys2.storename) {
self.storename = storename
self.quantity = try container2.decode(Int.self, forKey: CodingKeys2.quantity)
self.id = try container2.decode(Int.self, forKey: CodingKeys2.id)
self.price = try container2.decode(Double.self, forKey: CodingKeys2.price)
} else {
throw DecodingError()
}
}
}


do {
let j1 = try JSONDecoder().decode(Model.self, from: json1)
print(j1)
let j2 = try JSONDecoder().decode(Model.self, from: json2)
print(j2)
} catch {
print(error)
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 7:33

























answered Nov 15 '18 at 7:00









GereonGereon

6,55641746




6,55641746













  • You shouldn't use Data as your model name. It's already a foundation type.

    – Desdenova
    Nov 15 '18 at 7:31






  • 1





    True, that can be confusing. Updated my answer.

    – Gereon
    Nov 15 '18 at 7:34











  • let json1 = """ { "shopname":"xxx", "quantity":4, "id":1, "price":200.00, } """.data(using: .utf8)! let json2 = """ { "storename":"xxx", "qty":4, "id":1, "amount":200.00, } """.data(using: .utf8)! Here need to pass my json response?? Kindly share some helpful url to know more about my concern.

    – Parthee Vs
    Nov 15 '18 at 7:46













  • That's just an example so you can try the code e.g. in a playground. In your code, you would use your JSON response data in place of my json1 or json2

    – Gereon
    Nov 15 '18 at 8:25













  • @Gereon Thanks You

    – Parthee Vs
    Nov 20 '18 at 13:32



















  • You shouldn't use Data as your model name. It's already a foundation type.

    – Desdenova
    Nov 15 '18 at 7:31






  • 1





    True, that can be confusing. Updated my answer.

    – Gereon
    Nov 15 '18 at 7:34











  • let json1 = """ { "shopname":"xxx", "quantity":4, "id":1, "price":200.00, } """.data(using: .utf8)! let json2 = """ { "storename":"xxx", "qty":4, "id":1, "amount":200.00, } """.data(using: .utf8)! Here need to pass my json response?? Kindly share some helpful url to know more about my concern.

    – Parthee Vs
    Nov 15 '18 at 7:46













  • That's just an example so you can try the code e.g. in a playground. In your code, you would use your JSON response data in place of my json1 or json2

    – Gereon
    Nov 15 '18 at 8:25













  • @Gereon Thanks You

    – Parthee Vs
    Nov 20 '18 at 13:32

















You shouldn't use Data as your model name. It's already a foundation type.

– Desdenova
Nov 15 '18 at 7:31





You shouldn't use Data as your model name. It's already a foundation type.

– Desdenova
Nov 15 '18 at 7:31




1




1





True, that can be confusing. Updated my answer.

– Gereon
Nov 15 '18 at 7:34





True, that can be confusing. Updated my answer.

– Gereon
Nov 15 '18 at 7:34













let json1 = """ { "shopname":"xxx", "quantity":4, "id":1, "price":200.00, } """.data(using: .utf8)! let json2 = """ { "storename":"xxx", "qty":4, "id":1, "amount":200.00, } """.data(using: .utf8)! Here need to pass my json response?? Kindly share some helpful url to know more about my concern.

– Parthee Vs
Nov 15 '18 at 7:46







let json1 = """ { "shopname":"xxx", "quantity":4, "id":1, "price":200.00, } """.data(using: .utf8)! let json2 = """ { "storename":"xxx", "qty":4, "id":1, "amount":200.00, } """.data(using: .utf8)! Here need to pass my json response?? Kindly share some helpful url to know more about my concern.

– Parthee Vs
Nov 15 '18 at 7:46















That's just an example so you can try the code e.g. in a playground. In your code, you would use your JSON response data in place of my json1 or json2

– Gereon
Nov 15 '18 at 8:25







That's just an example so you can try the code e.g. in a playground. In your code, you would use your JSON response data in place of my json1 or json2

– Gereon
Nov 15 '18 at 8:25















@Gereon Thanks You

– Parthee Vs
Nov 20 '18 at 13:32





@Gereon Thanks You

– Parthee Vs
Nov 20 '18 at 13:32













0














Use like



struct modelClass : Codable {

let amount : Float?
let id : Int?
let price : Float?
let qty : Int?
let quantity : Int?
let shopname : String?
let storename : String?


enum CodingKeys: String, CodingKey {
case amount = "amount"
case id = "id"
case price = "price"
case qty = "qty"
case quantity = "quantity"
case shopname = "shopname"
case storename = "storename"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
amount = try values.decodeIfPresent(Float.self, forKey: .amount)
id = try values.decodeIfPresent(Int.self, forKey: .id)
price = try values.decodeIfPresent(Float.self, forKey: .price)
qty = try values.decodeIfPresent(Int.self, forKey: .qty)
quantity = try values.decodeIfPresent(Int.self, forKey: .quantity)
shopname = try values.decodeIfPresent(String.self, forKey: .shopname)
storename = try values.decodeIfPresent(String.self, forKey: .storename)
}


}





share|improve this answer
























  • Thanks @vikash.. is it is possible to set value in single variable. like qty and quantity both are stored in same variable based on availability

    – Parthee Vs
    Nov 15 '18 at 7:11


















0














Use like



struct modelClass : Codable {

let amount : Float?
let id : Int?
let price : Float?
let qty : Int?
let quantity : Int?
let shopname : String?
let storename : String?


enum CodingKeys: String, CodingKey {
case amount = "amount"
case id = "id"
case price = "price"
case qty = "qty"
case quantity = "quantity"
case shopname = "shopname"
case storename = "storename"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
amount = try values.decodeIfPresent(Float.self, forKey: .amount)
id = try values.decodeIfPresent(Int.self, forKey: .id)
price = try values.decodeIfPresent(Float.self, forKey: .price)
qty = try values.decodeIfPresent(Int.self, forKey: .qty)
quantity = try values.decodeIfPresent(Int.self, forKey: .quantity)
shopname = try values.decodeIfPresent(String.self, forKey: .shopname)
storename = try values.decodeIfPresent(String.self, forKey: .storename)
}


}





share|improve this answer
























  • Thanks @vikash.. is it is possible to set value in single variable. like qty and quantity both are stored in same variable based on availability

    – Parthee Vs
    Nov 15 '18 at 7:11
















0












0








0







Use like



struct modelClass : Codable {

let amount : Float?
let id : Int?
let price : Float?
let qty : Int?
let quantity : Int?
let shopname : String?
let storename : String?


enum CodingKeys: String, CodingKey {
case amount = "amount"
case id = "id"
case price = "price"
case qty = "qty"
case quantity = "quantity"
case shopname = "shopname"
case storename = "storename"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
amount = try values.decodeIfPresent(Float.self, forKey: .amount)
id = try values.decodeIfPresent(Int.self, forKey: .id)
price = try values.decodeIfPresent(Float.self, forKey: .price)
qty = try values.decodeIfPresent(Int.self, forKey: .qty)
quantity = try values.decodeIfPresent(Int.self, forKey: .quantity)
shopname = try values.decodeIfPresent(String.self, forKey: .shopname)
storename = try values.decodeIfPresent(String.self, forKey: .storename)
}


}





share|improve this answer













Use like



struct modelClass : Codable {

let amount : Float?
let id : Int?
let price : Float?
let qty : Int?
let quantity : Int?
let shopname : String?
let storename : String?


enum CodingKeys: String, CodingKey {
case amount = "amount"
case id = "id"
case price = "price"
case qty = "qty"
case quantity = "quantity"
case shopname = "shopname"
case storename = "storename"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
amount = try values.decodeIfPresent(Float.self, forKey: .amount)
id = try values.decodeIfPresent(Int.self, forKey: .id)
price = try values.decodeIfPresent(Float.self, forKey: .price)
qty = try values.decodeIfPresent(Int.self, forKey: .qty)
quantity = try values.decodeIfPresent(Int.self, forKey: .quantity)
shopname = try values.decodeIfPresent(String.self, forKey: .shopname)
storename = try values.decodeIfPresent(String.self, forKey: .storename)
}


}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 6:46









Vikash KumarVikash Kumar

1,0441718




1,0441718













  • Thanks @vikash.. is it is possible to set value in single variable. like qty and quantity both are stored in same variable based on availability

    – Parthee Vs
    Nov 15 '18 at 7:11





















  • Thanks @vikash.. is it is possible to set value in single variable. like qty and quantity both are stored in same variable based on availability

    – Parthee Vs
    Nov 15 '18 at 7:11



















Thanks @vikash.. is it is possible to set value in single variable. like qty and quantity both are stored in same variable based on availability

– Parthee Vs
Nov 15 '18 at 7:11







Thanks @vikash.. is it is possible to set value in single variable. like qty and quantity both are stored in same variable based on availability

– Parthee Vs
Nov 15 '18 at 7:11




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313725%2fdecoding-two-different-json-responses-in-one-model-class-using-codable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python