How to get the JSON Object “translated ”in a Swift Object. PART 2.

Lorenzo Piombini
3 min readFeb 26, 2021

I have committed myself to master Swift language and every day I am learning something new. Convert Java Script Object Notation(JSON) into custom object in Swift is really important to understand and I think that in the part1 of this topic I explained how to decode JSON into your Swift object, however I did not know there was a great help to do so.

Even though I believe that keep the name if your object properties equal to the JSON properties is the best practice and I would not suggest to do the opposite, we can actually use a protocol (CodingKey) with a method like init(from decoder: Decoder).

you can even change your object’s name properties from the JSON one, which I don’t think is a good practice, but in some cases could be helpful.

Let’s work on our weather API response as per the first article, here the object from the API, on the right side our custom objet for the current propriety :

struct Current:Codable {

var dayTime: Int (see below )
var sunrise: Int
var sunset: Int
var temp: Double
var feels_like: Double
var pressure: Int
var humidity: Int
var dew_point: Double
var uvi: Double
var clouds: Int
var visibility: Int?
var wind_speed: Double
var wind_deg: Int
var wind_gust: Double?

var weather: [weather]

}

now, if you want to call the properties in a different way you will have to add an enum with String protocol and Codingkey Protocol, here for example I will call dt dayTime instead:

enum CodingKeys: String, CodingKey {
case dayTime = “dt”
case sunrise
case sunset
case temp
case feels_like
case pressure
case humidity
case dew_point
case uvi
case clouds
case visibility
case wind_speed
case wind_deg
case wind_gust
case weather
}

after this we can use the init() method we will use the Decoder container propriety to store the Key value from the enum declared above under the name CodingKeys :

init(from decoder: Decoder) throws {
let valueContainer = try decoder.container(keyedBy: CodingKeys.self)
self.dayTime = try valueContainer.decode(String.self, forKey:CodingKeys.dayTime)

}

you have to do the same for each variable, your struct should look like this :

i think it is really import to point out that Adding Codable to the inheritance list for Current triggers an automatic conformance that satisfies all of the protocol requirements from Encodable and Decodable.

basically you do not need to write code to handle JSON if you use the Codable protocol as I explained in part1, however, this code can be helpful if you want to give a more meaningful name to your object’s proprieties, like in code above, dt has no meaning and at first glance you cannot understand what this variable stores if you are a new developer ready to work on the projects.

here the link to Apple developer documentation :
https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types

also I will suggest to download App Development with Swift from the app Store, a good guide to coding apps.

Thank you for reading
as always hope the article has been helpful.

--

--