Parsing JSON Just Became Super Easy in Swift 4 with Decodable

Transforming JSON into model objects in your apps just became a whole lot easier with the introduction of Decodable in Swift 4. In today’s lesson, I want to walk through the old way of using JSONSerialization and then show you the new JSONDecoder method. We can retire a bunch of JSON serializer pods now that we have this feature natively supported.

URLs for JSON blobs:

Completed Source Code

Instagram Firebase Course

Facebook Group

iOS Basic Training Course

Follow me on Twitter:


Thet Htun says:

thank you. it was really helpful.

Digvijay Bhakuni says:

Decoder Not working for Date field ? awesome video can u help

West Kraemer says:

How do you display the called information in a label on the storyboard?

Karen Minasyan says:

Great tutorial, thank you…

Sangeeta Sharma says:

Hey Brian!
Thank for this tutorial, before watching this video I was writing code like it is 2015. Thank to you I got update.
While trying different JSON to parse with this method I came across a Complex JSON (find JSON here – ) which I’m not able to parse using Decodable protocol. Brian can you please help me with that?
Thanks in advance.

flocco says:

the statement guard let data = data is really killing me

StrawHat Pirates says:

Thank you bro for the video

Atirek Sharma says:

Hello, I just want to ask..Should I user more libraries in the project or create native custom code..For ex: Fetching response from API should I use alamofire or normal native code like URLSession ?

Syed Naqvi says:

Thanks brother its awesome. 🙂

Rajko Podinic says:

Im placing all the data in a table view but getting the following error

typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: “Expected to decode Array but found a dictionary instead.

Any chance you could tell me what my code needs? Please see below:
@objc func fetchJSON() {
let urlString = “”

guard let url = URL(string: urlString) else {return}
URLSession.shared.dataTask(with: url) {(data, _, err) in
DispatchQueue.main.async {
if let err = err {
print(“Failed to get data from URL:”, err)

guard let data = data else {return}

do {
let decoder = JSONDecoder()
self.articles = try decoder.decode([Article].self, from: data)
} catch let jsonErr {
print(“Failed to decode:”, jsonErr)

นายกฤษณะ ร่องแก้ว says:

Thank you. this video tutorial. It’s worked. 🙂

Rishi Lund says:

one of the best tutorial for API’s , thanks man

Ioana says:

Super video! Thx!

Eli Chitrit says:

You’re just a magician

Rhidlor says:

fuck i hate this language, im going to hang myself

Ty Mondragon says:

I’m new to Swift and have spent hours trying to parse an array of object in JSON. Thank you sooooooo much!

ali hajian says:

How can i deserialize token?

vishav ios says:

how to parson json .post method in swagger client

mywork email says:

is it still worth using Alamofire? and how do you use JSONDecodable with Alamofire? I have found this

Nikhil Sawant says:

Thank you so much, you saved my day


Help be print the courses or simply help me print data in the array course

Antonio Velazco says:

Hi, thank you so much for this video, it helped a lot for my final project, but I still have one problem, I need to place data on a variable but since the network call is async, my variable return empty and before the call is over, how can I fix this

ismail tosun says:

so what about additional variables ? Could you video about additional variables ?

Liz G says:

Hi Brian, I need your help with something, is there a way to email you my issue?

ali hajian says:

How can i deserialize token?

David Guillet says:

Great video thank you for sharing

Sreekanth M says:

Thank you for your classes. I am getting an error “The data couldn’t be read because it isn’t in the correct format.” while using single line Json parsing. Is there any possibility to find, which key raising this error.

Иван Дягилев says:


José Vera says:

Hi. Great tutorial!!! I only have one problem, I have a web service and the data that it returns is OK (I use Postman to test it and in Android Studio is OK too), but in Xcode some fields returns as “nil” and I do not know why.

Said'S_LogS says:

As a developer with big experience, can you tell how frequent is there a need for you to use CocoaPods ? I mean just generally speaking, do you use 3rd party libraries in all your projects ?

Govind Suryawanshi says:

Great video, Really helps me lot…….

Leonard Choo says:

Best, most helpful video out there.. thank you so much. I subscribed.

Polu says:

Very nice tutorial sir. JSON parsing is really very easy now. But I have one question. How can we add some default value to the optional property? Do we need to write any code inside the class/structure/enum definition..??

Hassan Mohammed says:

am not able to access the value form can you please help me

Said'S_LogS says:

Nice, simple and easily understandable tutorial ! keep going bro

Celalettin UYAR says:

This tutorial really helpful for me thanks:)

Markiyan Pryssiaznuk says:

hey man, awseome tutorial but i have an error that occerps when i’m traying to parse some big array of json, could you help plz?
Here’s code and i’m trying to receive the ids of all photos from picsum
let jsonUrl = “”
guard let url = URL(string: jsonUrl) else { return }
URLSession.shared.dataTask(with: url) { (data, responce, err) in
guard let data = data else { return }
do {
let listMine = try JSONDecoder().decode([List].self, from: data)
} catch let jsonErr {

ismail tosun says:

great thanks a lot ! 🙂


Excuse me sir, How could I handle some null value in the array JSON.
For example
id : 1 ,
name : ABCD,
location[13.22222 , 101.22222]
id : 2,
name : CDEF,
location [ null , null ]
id : 3 ,
name : HHHH,
location[14.2342 , 101.23222]

And this is my script to get data

func getAllLocationDataToStorage(dataJSON : JSON){
let count = dataJSON.count
locationStorages = []
for index in 0..]
if (jsonArray[0][“locations”]) != nil {
let vehicleLocation = jsonArray[0][“locations”] as! NSArray
storage.latitude = vehicleLocation[0] as! Double
storage.longitude = vehicleLocation[1] as! Double
print(“bad json”)
}catch let error as NSError{
print(“Error JSON Serialization : ” , error)

And I got error when some null

Could not cast value of type ‘NSNull’ (0x11322f850) to ‘NSNumber’ (0x111c42b40).
2018-09-17 13:10:48.101714+0700 DGFTrackingIOSProject[6702:779736] Could not cast value of type ‘NSNull’ (0x11322f850) to ‘NSNumber’ (0x111c42b40).

Aaron Treinish says:

How would you do it if you have a paged API and JSON data? The API I’m working with I can get all the data from the first page, but not the other pages.

thinkmovers magic says:

Great video sir your all videos are very helpfull thank you

Valeria - says:

Hello, I need to download a dpf from a url … it is a specific pdf that is posted on a web site … I can show it in a webview but not download it on the phone … do you have any idea how to do it? I’m searching the internet but I can not find something specific …


“date”: “9-07-2018”,
“data”: {
“total”: 1150,
“day_items”: [
“amount”: 55,
“category”: “Gift”,
“colorcode”: “#ba68c8”,
“contents”: “Keychain from Archies”,
“id”: “hash of data”,
“payment_method”: “card”,
“receipt_url”: “url”,
“time”: 1529489815566

I want to know how to decode “data” in this above JSON in

let dayEvent = try JSONDecoder().decode(Decodable.Protocol, from: data)

jason villegas says:

I don’t get how this works in some case..I can use AFNEtworking with something like this `apiRequest(withMethod: “GET”, toPath: nil, atEndpoint: realURL, withJSON: nil, success: { (operation, JSON) in ….. and get data back if I build the URL myself…but when I use the URLSession.shared.dataTask and do the same thing and build the URL myself..I am getting a 401 unauthorized on my response. Mind you the URLs that are being created are the exact same. is the URLSession.shared.dataTask not doing a ‘get’ request? What am I missing here. I really want to be able to use the newer style vs the old style

TimKan12 says:

Thanks so much!!! Took me no time at all to get the job done!

Jadiel Alfonso says:

Thank you for this video Brian! I was wondering how can I update a UILabel to print out “”. Thanks for the help.

 Write a comment


Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!