title: How to fix invalid JSON response in swift when using get request in Alamofire?
body: <p>I am creating an app wherein it pulls <code>PatientList</code> from API Server and it will display to a <code>TableView</code>. Upon checking, it returns <code>200</code> status code but falls to <code>invalidJSON</code> error. But when I checked in <strong>Postman</strong>, it returns <code>200</code> status code and pulls the records properly. I am quite confuse which part of my codes causes the error since I am new in swift. I am seeking help to solve the issue. Below are my sample codes for your references. Thank you so much in advance.</p>
  
  <p><strong>Patient.swift</strong></p>
  
  <pre><code>struct Patient: Codable {
      let hospitalNumber: Int
      let patientName: String
      let totalAmount: Double
  
  enum CodingKeys: String, CodingKey {
      case hospitalNumber = "hospitalNumber"
      case patientName = "patientName"
      case totalAmount = "totalAmount"
     }
  }
  </code></pre>
  
  <p><strong>APIService.swift</strong></p>
  
  <pre><code>struct PatientList {
      typealias getPatientListTaskCompletion = (_ patientListperPayout: [Patient]?, _ error: NetworkError?) -&gt; Void
  
  
      static func getPatientList(doctorNumber: Int, periodId: Int, completion: @escaping getPatientListTaskCompletion) {
  
          guard let patientPerPayoutURL = URL(string: "\(Endpoint.Patient.patientPerPayout)?periodId=\(periodId)&amp;doctorNumber=\(doctorNumber)") else {
              completion(nil, .invalidURL)
              return
          }
  
          let sessionManager = Alamofire.SessionManager.default
          sessionManager.session.getAllTasks { (tasks) in
              tasks.forEach({ $0.cancel() })
          }
  
          Alamofire.request(patientPerPayoutURL, method: .get, encoding: JSONEncoding.default).responseJSON { (response) in
              guard HelperMethods.reachability(responseResult: response.result) else {
                  completion(nil, .noNetwork)
                  return
              }
  
              guard let statusCode = response.response?.statusCode else {
                  completion(nil, .noStatusCode)
                  return
              }
  
              switch(statusCode) {
              case 200:
                  guard let jsonData = response.data else{
                      completion(nil, .invalidJSON)
                      print(statusCode)
                      return
                  }
  
                  let decoder = JSONDecoder()
  
                  do {
                      let patientListArray = try decoder.decode([Patient].self, from: jsonData)
                      let sortedPatientListArray = patientListArray.sorted(by: { $0.patientName &lt; $1.patientName })
                      completion(sortedPatientListArray, nil)
                  }catch{
                      completion(nil, .invalidJSON)
                      print(statusCode)
                  }
              case 400:
                  completion(nil, .badRequest)
              case 404:
                  completion(nil, .noRecordFound)
              default:
                  print("UNCAPTURED STATUS CODE FROM getPatientList\nSTATUS CODE: \(statusCode)")
                  completion(nil, .uncapturedStatusCode)
                  }
              }
          }
  </code></pre>
  
  <p><strong>Controller.swift</strong></p>
  
  <pre><code>var patientList: [Patient]! {
      didSet {
         performSegue(withIdentifier: patientListIdentifier, sender: self)
      }
  }
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      self.latestCreditedAmountTableView.dataSource = self
      self.latestCreditedAmountTableView.delegate = self
  
      configureTableViewCell()
      showTotalCreditedAmount()
       getDoctorPayoutSummary(doctorNumber: doctorNumber)
  
  }
  
  func getDoctorPayoutSummary(doctorNumber: Int) {
    self.payoutSummary = payoutSummaryDetails
          self.taxRateVatRateLabel.text = "\(self.payoutSummary.taxRate) / \(self.payoutSummary.vatRate)"
          self.getPatientList()
          self.latestCreditedAmountTableView.reloadData()
          return
  }
  
   func getPatientList() {
  
      APIService.PatientList.getPatientList(doctorNumber: doctorNumber, periodId: currentRemittance.periodId) { (patientListArray, error) in
          guard let patientListPerPayout = patientListArray, error == nil else {
              if let networkError = error {
                  switch networkError {
                  case .noRecordFound:
                      let alertController = UIAlertController(title: "No Record Found", message: "You don't have current payment remittance", preferredStyle: .alert)
                      alertController.addAction(UIAlertAction(title: "OK", style: .default))
                      self.present(alertController, animated: true, completion: nil)
                  case .noNetwork:
                      let alertController = UIAlertController(title: "No Network", message: "\(networkError.rawValue)", preferredStyle: .alert)
  
                      alertController.addAction(UIAlertAction(title: "OK", style: .default))
                      self.present(alertController, animated: true, completion: nil)
                  default:
                      let alertController = UIAlertController(title: "Error", message: "There is something went wrong. Please try again", preferredStyle: .alert)
                      alertController.addAction(UIAlertAction(title: "OK", style: .default))
                      self.present(alertController, animated: true, completion: nil)
                  }
              }
              return
          }
  
          self.patientList = patientListPerPayout
  
          return
  
      }
  }
  </code></pre>
  
  <p><strong>JSON Response</strong></p>
  
  <pre><code>[
     {
     "hospitalNumber": null,
     "patientName": null,
     "totalAmount": 31104
     },
     {
     "hospitalNumber": "",
     "patientName": "LastName, FirstName",
     "totalAmount": 3439.8
     }
  ]
  </code></pre>