skip to Main Content

I have a JSON file I am trying to extract data from and I keep getting errors that I cannot resolve for some reason, any advice?

Here is how I am running it in playgrounds

import UIKit

struct GETprojects: Codable {
    var projectGetResult: Bool
    var projectGetProjects: ProjectGetProjects
}

struct ProjectGetProjects: Codable {
    var projects: [Projects] = Array()
}

struct Projects: Codable {
    var id: String
    var name:  String
    var status:  String
    var category:  String
    var type:  String
    var updated:  String
    var value:  String
    var units:  String
    var address:  Address
    var contacts: Contacts
}

struct Address: Codable {
    var  erf: String
    var  street: String
    var  street2: String
    var  town: String
    var  city: String
    var  region: String
    var  postalCode: String
}

struct Contacts: Codable {
    var professional, contractor: Contact
}

struct Contact: Codable {
   var  id: String
    var  name: String
    var  company: String
    var  phone: String
    var  mobile: String
    var  email: String
    var  type: String
}
'''

let JSONString = """
{
    "projectGetResult": true,
    "projectGetProjects": [
      {
          "id": "123-uni-que",
          "name": "Amazon",
          "status": "Planning",
          "category": "Commercial",
          "type": "Office Block",
          "updated": "21 March 2023",
          "value": 100000000,
          "units": 1,
          "address": {
              "erf": "101-RE",
              "street": "First Avenue",
              "street2": "",
              "town": "Cape Town",
              "city": "Cape Town",
              "region": "Western Cape",
              "postalCode": "111"
          },
          "contacts": {
              "professional": [
                  {
                  "id": "pr123",
                  "name":"Charles Darwin",
                  "company":"Darwin Designs",
                  "phone":"011 234 5678",
                  "mobile":"071 234 5678",
                  "email":"[email protected]",
                  "type":"Architect"
                  },
                  {
                  "id": "pr234",
                  "name":"Bill Hutchins",
                  "company":"Hutchins Engineering",
                  "phone":"012 345 6789",
                  "mobile":"072 345 6789",
                  "email":"[email protected]",
                  "type":"Engineer"
                  },
    {
    "id": "pr345",
    "name":"Kevin Counter",
    "company":"KCQS",
    "phone":"013 456 7890",
    "mobile":"073 456 7890",
    "email":"[email protected]",
    "type":"Quantity Surveyor"
    }
    ],
    "contractor": [
    {
    "id": "con123",
    "name":"Leonardo da Vinci",
    "company":"MegaBuild",
    "phone":"014 567 8901",
    "mobile":"074 567 8901",
    "email":"[email protected]",
    "type":"Main"
    },
    {
    "id": "con234",
    "name":"Steve Cleer",
                  "company":"SC Civils",
                  "phone":"015 678 9012",
                  "mobile":"075 678 9012",
                  "email":"[email protected]",
                  "type":"Civil"
                  },
                  {
                      "id": "con345",
                      "name":"Warren Fright",
                      "company":"Electrified",
                      "phone":"016 789 0123",
                      "mobile":"076 789 0123",
                      "email":"@sparky.com",
                      "type":"Electrical"
                      }
              ]
          }
      },
      {
          "id": "234-uni-que",
          "name": "TBC SA",
          "status": "On Site",
          "category": "Commercial",
          "type": "Office Block",
          "updated": "21 March 2023",
          "value": 200000000,
          "units": 2,
          "address": {
              "erf": "102-RE",
              "street": "Second Avenue",
              "street2": "Corner of 4th",
              "town": "Stellenbosch",
              "city": "Cape Town",
              "region": "Western Cape",
              "postalCode": "222"
          },
          "contacts":  {
                  "professional": [
                      {
                      "id": "pr123",
                      "name":"Charles Darwin",
                      "company":"Darwin Designs",
                      "phone":"011 234 5678",
                      "mobile":"071 234 5678",
                      "email":"[email protected]",
                      "type":"Architect"
                      },
                      {
                      "id": "pr234",
                      "name":"Bill Hutchins",
                      "company":"Hutchins Engineering",
                      "phone":"012 345 6789",
                      "mobile":"072 345 6789",
                      "email":"[email protected]",
                      "type":"Engineer"
                      },
                      {
                          "id": "pr345",
                          "name":"Kevin Counter",
                          "company":"KCQS",
                          "phone":"013 456 7890",
                          "mobile":"073 456 7890",
                          "email":"[email protected]",
                          "type":"Quantity Surveyor"
                          }
                  ],
                  "contractor": [
                      {
                      "id": "con123",
                      "name":"Leonardo da Vinci",
                      "company":"MegaBuild",
                      "phone":"014 567 8901",
                      "mobile":"074 567 8901",
                      "email":"[email protected]",
                      "type":"Main"
                      },
                      {
                      "id": "con234",
                      "name":"Steve Cleer",
                      "company":"SC Civils",
                      "phone":"015 678 9012",
                      "mobile":"075 678 9012",
                      "email":"[email protected]",
                      "type":"Civil"
                      },
                      {
                          "id": "con345",
                          "name":"Warren Fright",
                          "company":"Electrified",
                          "phone":"016 789 0123",
                          "mobile":"076 789 0123",
                          "email":"@sparky.com",
                          "type":"Electrical"
                          }
                  ]
              }
      },
      {
          "id": "345-uni-que",
          "name": "Rolling Stock",
          "status": "Out to Tender",
          "category": "Commercial",
          "type": "Office Block",
          "updated": "21 March 2023",
          "value": 300000000,
          "units": 3,
          "address": {
              "erf": "103-RE",
              "street": "Third Avenue",
              "street2": "",
              "town": "Swellendam",
              "city": "Drakenstein",
              "region": "Western Cape",
              "postalCode": "333"
          },
          "contacts":  {
                  "professional": [
                      {
                      "id": "pr123",
                      "name":"Charles Darwin",
                      "company":"Darwin Designs",
                      "phone":"011 234 5678",
                      "mobile":"071 234 5678",
                      "email":"[email protected]",
                      "type":"Architect"
                      },
                      {
                      "id": "pr234",
                      "name":"Bill Hutchins",
                      "company":"Hutchins Engineering",
                      "phone":"012 345 6789",
                      "mobile":"072 345 6789",
                      "email":"[email protected]",
                      "type":"Engineer"
                      },
    {
    "id": "pr345",
    "name":"Kevin Counter",
    "company":"KCQS",
    "phone":"013 456 7890",
    "mobile":"073 456 7890",
    "email":"[email protected]",
    "type":"Quantity Surveyor"
    }
    ],
    "contractor": [
    {
    "id": "con123",
    "name":"Leonardo da Vinci",
    "company":"MegaBuild",
    "phone":"014 567 8901",
    "mobile":"074 567 8901",
    "email":"[email protected]",
    "type":"Main"
    },
    {
    "id": "con234",
    "name":"Steve Cleer",
    "company":"SC Civils",
    "phone":"015 678 9012",
    "mobile":"075 678 9012",
    "email":"[email protected]",
    "type":"Civil"
    },
    {
    "id": "con345",
    "name":"Warren Fright",
    "company":"Electrified",
    "phone":"016 789 0123",
    "mobile":"076 789 0123",
    "email":"@sparky.com",
    "type":"Electrical"
    }
    ]
    }}]}
"""
print(JSONString)
let jsonData = JSONString.data(using: .utf8)!
do {
  let decoder = JSONDecoder()
  let tableData = try decoder.decode(GETprojects.self, from: jsonData)
  print(tableData)
    print("Rows in array: (tableData.projectGetProjects.count)")
}
catch {
  print (error)
}`

getting the error:

**Value of type 'GETprojects' has no member 'GETprojects'**
at: print("Rows in array: (tableData.GETprojects.projectGetProjects.count)")

No matter how I configure the tableData I get a similar error, what am I missing?

2

Answers


  1. Maybe you’ve created the wrong model.
    Try this model and follow this website to create a model. https://app.quicktype.io/

    // MARK: - GETprojects
    struct GETprojects: Codable {
        let projectGetResult: Bool?
        let projectGetProjects: [ProjectGetProject]?
    }
    
    // MARK: - ProjectGetProject
    struct ProjectGetProject: Codable {
        let id, name, status, category: String?
        let type, updated: String?
        let value, units: Int?
        let address: Address?
        let contacts: Contacts?
    }
    
    // MARK: - Address
    struct Address: Codable {
        let erf, street, street2, town: String?
        let city, region, postalCode: String?
    }
    
    // MARK: - Contacts
    struct Contacts: Codable {
        let professional, contractor: [Contractor]?
    }
    
    // MARK: - Contractor
    struct Contractor: Codable {
        let id, name, company, phone: String?
        let mobile, email, type: String?
    }
    
    
      print(JSONString)
      let jsonData = JSONString.data(using: .utf8)!
      do {
        let decoder = JSONDecoder()
        let tableData = try decoder.decode(GETprojects.self, from: jsonData)
        print(tableData)
          print("Rows in array: (tableData.projectGetProjects?.count)")
      }
      catch {
        print (error)
      }
      
    
    Login or Signup to reply.
  2. struct Welcome: Codable {
        let projectGetResult: Bool
        let projectGetProjects: [ProjectGetProject]
    }
    
    struct ProjectGetProject: Codable {
        let id, name, status, category: String
        let type, updated: String
        let value, units: Int
        let address: Address
        let contacts: Contacts
    }
    
    struct Address: Codable {
        let erf, street, street2, town: String
        let city, region, postalCode: String
    }
    
    struct Contacts: Codable {
        let professional, contractor: [Contractor]
    }
    
    struct Contractor: Codable {
        let id, name, company, phone: String
        let mobile, email, type: String
    }
    
    
    class ViewController: UIViewController {
        
        let stringThing = """
        {
            "projectGetResult": true,
            "projectGetProjects": [
              {
                  "id": "123-uni-que",
                  "name": "Amazon",
                  "status": "Planning",
                  "category": "Commercial",
                  "type": "Office Block",
                  "updated": "21 March 2023",
                  "value": 100000000,
                  "units": 1,
                  "address": {
                      "erf": "101-RE",
                      "street": "First Avenue",
                      "street2": "",
                      "town": "Cape Town",
                      "city": "Cape Town",
                      "region": "Western Cape",
                      "postalCode": "111"
                  },
                  "contacts": {
                      "professional": [
                          {
                          "id": "pr123",
                          "name":"Charles Darwin",
                          "company":"Darwin Designs",
                          "phone":"011 234 5678",
                          "mobile":"071 234 5678",
                          "email":"[email protected]",
                          "type":"Architect"
                          },
                          {
                          "id": "pr234",
                          "name":"Bill Hutchins",
                          "company":"Hutchins Engineering",
                          "phone":"012 345 6789",
                          "mobile":"072 345 6789",
                          "email":"[email protected]",
                          "type":"Engineer"
                          },
            {
            "id": "pr345",
            "name":"Kevin Counter",
            "company":"KCQS",
            "phone":"013 456 7890",
            "mobile":"073 456 7890",
            "email":"[email protected]",
            "type":"Quantity Surveyor"
            }
            ],
            "contractor": [
            {
            "id": "con123",
            "name":"Leonardo da Vinci",
            "company":"MegaBuild",
            "phone":"014 567 8901",
            "mobile":"074 567 8901",
            "email":"[email protected]",
            "type":"Main"
            },
            {
            "id": "con234",
            "name":"Steve Cleer",
                          "company":"SC Civils",
                          "phone":"015 678 9012",
                          "mobile":"075 678 9012",
                          "email":"[email protected]",
                          "type":"Civil"
                          },
                          {
                              "id": "con345",
                              "name":"Warren Fright",
                              "company":"Electrified",
                              "phone":"016 789 0123",
                              "mobile":"076 789 0123",
                              "email":"@sparky.com",
                              "type":"Electrical"
                              }
                      ]
                  }
              },
              {
                  "id": "234-uni-que",
                  "name": "TBC SA",
                  "status": "On Site",
                  "category": "Commercial",
                  "type": "Office Block",
                  "updated": "21 March 2023",
                  "value": 200000000,
                  "units": 2,
                  "address": {
                      "erf": "102-RE",
                      "street": "Second Avenue",
                      "street2": "Corner of 4th",
                      "town": "Stellenbosch",
                      "city": "Cape Town",
                      "region": "Western Cape",
                      "postalCode": "222"
                  },
                  "contacts":  {
                          "professional": [
                              {
                              "id": "pr123",
                              "name":"Charles Darwin",
                              "company":"Darwin Designs",
                              "phone":"011 234 5678",
                              "mobile":"071 234 5678",
                              "email":"[email protected]",
                              "type":"Architect"
                              },
                              {
                              "id": "pr234",
                              "name":"Bill Hutchins",
                              "company":"Hutchins Engineering",
                              "phone":"012 345 6789",
                              "mobile":"072 345 6789",
                              "email":"[email protected]",
                              "type":"Engineer"
                              },
                              {
                                  "id": "pr345",
                                  "name":"Kevin Counter",
                                  "company":"KCQS",
                                  "phone":"013 456 7890",
                                  "mobile":"073 456 7890",
                                  "email":"[email protected]",
                                  "type":"Quantity Surveyor"
                                  }
                          ],
                          "contractor": [
                              {
                              "id": "con123",
                              "name":"Leonardo da Vinci",
                              "company":"MegaBuild",
                              "phone":"014 567 8901",
                              "mobile":"074 567 8901",
                              "email":"[email protected]",
                              "type":"Main"
                              },
                              {
                              "id": "con234",
                              "name":"Steve Cleer",
                              "company":"SC Civils",
                              "phone":"015 678 9012",
                              "mobile":"075 678 9012",
                              "email":"[email protected]",
                              "type":"Civil"
                              },
                              {
                                  "id": "con345",
                                  "name":"Warren Fright",
                                  "company":"Electrified",
                                  "phone":"016 789 0123",
                                  "mobile":"076 789 0123",
                                  "email":"@sparky.com",
                                  "type":"Electrical"
                                  }
                          ]
                      }
              },
              {
                  "id": "345-uni-que",
                  "name": "Rolling Stock",
                  "status": "Out to Tender",
                  "category": "Commercial",
                  "type": "Office Block",
                  "updated": "21 March 2023",
                  "value": 300000000,
                  "units": 3,
                  "address": {
                      "erf": "103-RE",
                      "street": "Third Avenue",
                      "street2": "",
                      "town": "Swellendam",
                      "city": "Drakenstein",
                      "region": "Western Cape",
                      "postalCode": "333"
                  },
                  "contacts":  {
                          "professional": [
                              {
                              "id": "pr123",
                              "name":"Charles Darwin",
                              "company":"Darwin Designs",
                              "phone":"011 234 5678",
                              "mobile":"071 234 5678",
                              "email":"[email protected]",
                              "type":"Architect"
                              },
                              {
                              "id": "pr234",
                              "name":"Bill Hutchins",
                              "company":"Hutchins Engineering",
                              "phone":"012 345 6789",
                              "mobile":"072 345 6789",
                              "email":"[email protected]",
                              "type":"Engineer"
                              },
            {
            "id": "pr345",
            "name":"Kevin Counter",
            "company":"KCQS",
            "phone":"013 456 7890",
            "mobile":"073 456 7890",
            "email":"[email protected]",
            "type":"Quantity Surveyor"
            }
            ],
            "contractor": [
            {
            "id": "con123",
            "name":"Leonardo da Vinci",
            "company":"MegaBuild",
            "phone":"014 567 8901",
            "mobile":"074 567 8901",
            "email":"[email protected]",
            "type":"Main"
            },
            {
            "id": "con234",
            "name":"Steve Cleer",
            "company":"SC Civils",
            "phone":"015 678 9012",
            "mobile":"075 678 9012",
            "email":"[email protected]",
            "type":"Civil"
            },
            {
            "id": "con345",
            "name":"Warren Fright",
            "company":"Electrified",
            "phone":"016 789 0123",
            "mobile":"076 789 0123",
            "email":"@sparky.com",
            "type":"Electrical"
            }
            ]
            }}]}
        """
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            do{
                let jsonData = stringThing.data(using: .utf8)!
                let result = try JSONDecoder().decode(Welcome.self, from: jsonData)
                print(result)
            }catch{
                print("error")
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search