UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 59 | Milestone project 13-15 | Decoding JSON from World Bank API

Forums > 100 Days of Swift

Hello everyone! I hope you're all well.

For this milestone project I wanted to use a World Bank endpoint instead of a file, to keep the info updated, but the format is different from the one we used in Project 7 (whitehouse petitions), which contained the info inside an array called "results", like so:

{
   "metadata":{
      "responseInfo":{
         "status":200,
         "developerMessage":"OK",
         "userMessage":"",
         "errorCode":"",
         "moreInfo":""
      },
     .....
   },
   "results":[
      {

The json I'm using has an array with 2 items and I interested in the second one that has the [Country]

[
  { 
    "page": 1,
    "pages": 6,
    "per_page": "50",
    "total": 297
  },
  [
    {
      "id": "ABW",
      "iso2Code": "AW",
      "name": "Aruba",
      "region": {
        "id": "LCN",
        "iso2code": "ZJ",
        "value": "Latin America & Caribbean "
      },

I am getting this error: "The data couldn’t be read because it is missing." when using the decoder like so:

do {
            let jsonCountries = try decoder.decode([Country].self, from: json)
            debugPrint(jsonCountries)
        } catch {
            debugPrint(error.localizedDescription)
        }

What am I doing wrong?

3      

The json that you have given is not correct. Can you give the web URL so can look at the json.

When using json you need to get the struct correct. I found Ducky very good to help with that and it free on the Mac App Store. This video Ducky Model Editor for Swift Developers - by Stewart Lynch explains how to use it.

4      

Thanks @NigelGee ! I would take a look at the video.

This is the link I am trying to use: https://api.worldbank.org/v2/country/all?format=json

And this is my current struct:

struct Country: Codable {
    var name: String
    var capitalCity: String
    var longitude: String
    var latitude: String
}

I ended up creating a local .txt file deleting the first item of the Array and reading it from the Bundle.main and it worked, but I was really looking forward to understand a little bit more about JSON and using the endpoint.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.