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

Lorenzo Piombini
4 min readJan 19, 2021

when is time to get the response from the HTTP request, it is not obvious how to figure out what it could the Object form the API be translated in Swift. we are getting a JSON format response from our request which is going to be something like this :

this is looking close to what in Swift could be a Dictionary, however it is not the right collection data type to translate this Json object. Swift has the protocol Codable, and you can use the function JSONDecoder().decode(your object, from: Api data response).
one of the issue that I wasn’t completely aware of was that if you set everything right about requesting the data from the API, if you are not careful to create an object looking exactly the same with our Json object from API, you will never get the data. You will lose time to understand what is wrong, maybe because you will think there is something wrong with the way you set up the HTTP request. if you take a look at this picture on the left side you will see a property “uvi”: 1.28, this property was crashing my Weather app. because I set this property as a Integer, since when the uvi is equal to zero the value will be just 0, which is an Integer, a small error like this may crash your app, so it is tremendously relevant that you create a STRUCT in swift that is like our JSON response. if we want to create an object in Swift storing the JSON in this picture we will have to do like this:

struct NameYouWantToGive:Codable {

var lat: Double
var lon: Double
var timezone: String
var timezone_offset: Int
var current: Current
}

the name of the variables has to match the JSON object, also, assign the protocol Codable at the struct. you probably notice that the variable current is of type Current which will be another struct called Current looking exactly the same to the JSON object, like the following:

struct Current:Codable {

var dt: Int
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]
}

and now we are going to create the same thing for the variables weather in the exact same way :

struct weather: Codable {
var id: Int
var main: String
var description: String
var icon: String
}

to better understand all the types of the JSON object’s fields I suggest to take a careful look at the API documentation, that will help a lot and saving you tons of time !
when you see this kind of object :

it will be a Struct called Coord with two variables lon and lat, both of them with types Double, and if coord will be a variable of a largest object it will be like var coord: Coord.

if you encounter an object like the weather that we’ve already seen above :

this will be a struct called weather :
struct weather: Codable {
var id: Int
var main: String
var description: String
var icon: String
}

but if it will be a variables in a largest object you will declare it like this: var weather:[weather], since it will be an array of type weather (you can understand that because the object has [ ] containing {}).

when workin with HTTP Request i find using Postman( www.postman.com ) really useful to check if the url is correct and all of the errors you might encounter setting up a request, and it is also helpful to take a look at the object you are getting.

To Wrap Up

  1. read the API documentation carefully
  2. Understand the type of object you are getting from API
  3. create then the STRUCT in swift to be the same thing of the object you are getting in the Response. variables names and types have to be the same as the Object!!!

thanks for reading, I Hope the article Helps you.

--

--