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:
https://api.letsbuildthatapp.com/jsondecodable/course
https://api.letsbuildthatapp.com/jsondecodable/courses
https://api.letsbuildthatapp.com/jsondecodable/website_description
https://api.letsbuildthatapp.com/jsondecodable/courses_missing_fields

Completed Source Code
https://www.letsbuildthatapp.com/course_video?id=1562

Instagram Firebase Course
https://www.letsbuildthatapp.com/course/instagram-firebase

Facebook Group
https://www.facebook.com/groups/1240636442694543/

iOS Basic Training Course
https://www.letsbuildthatapp.com/basic-training

Follow me on Twitter: https://twitter.com/buildthatapp

26 thoughts on “Parsing JSON Just Became Super Easy in Swift 4 with Decodable”

  1. In the video you go print(websiteDescription.name, websiteDescription.description)

    But if you also wanted to print the first course how would you go about doing that?

    Reply
  2. Hi Brian. Great tutorial. Thank you. Can you show us how to view the data in a table View Cell? Thanks!

    Reply
  3. your videos are so awesome. Please make a video on how to upload multiple images to REST server. Searched a lot in youtube and also in stack overflow but couldn't find any proper solution. Thanks

    Reply
  4. struct jsonstruct:Decodable {
    let name:String
    let capital:String
    let alpha2Code:String
    let alpha3Code:String
    let region:String
    let subregion:String

    }
    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableview: UITableView!

    var arrdata = [jsonstruct]()
    override func viewDidLoad() {
    super.viewDidLoad()
    getdata()
    }

    func getdata(){
    let url = URL(string: "https://restcountries.eu/rest/v2/all")
    URLSession.shared.dataTask(with: url!) { (data, response, error) in
    do{if error == nil{
    self.arrdata = try JSONDecoder().decode([jsonstruct].self, from: data!)

    for mainarr in self.arrdata{
    //print(mainarr.name,":",mainarr.capital,":",mainarr.alpha3Code)
    DispatchQueue.main.async {
    self.tableview.reloadData()
    }

    }
    }

    }catch{
    print("Error in get json data")
    }

    }.resume()
    }

    //TableView

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.arrdata.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
    cell.lblname.text = "Name : (arrdata[indexPath.row].name)"
    cell.lblcapital.text = "Capital : (arrdata[indexPath.row].capital)"
    return cell
    }

    Reply
  5. Timestamp to the 1 line of code he mentioned in the title? I'm watching the video and I'm looking at about 70 lines of code so far.

    Reply
  6. I want to take backup of contact list in my app, If you could help me please give me an idea. I have already accessed my contacts list in app and taken the data in a dictionary; further where to send this data and save it, I have no idea.Would it be useful if I send it to gmail in the form of json and then access accordingly??

    Reply
  7. It has become hard for me to pass by any of your videos without giving it a *LIKE*.
    Some awesome content here.

    Reply
  8. how can you think all json objects include same length parameters ? my json tree is complicated. some categories has 10 keys, some has 20 keys with 10 value. this is bullshit. Click bait

    Reply
  9. Do I need to add all properties in the Decodable Struct? Can I just add only the properties that I want to use/parse?

    Reply

Leave a Comment