title: How to store JSON decodable values into Coredata using Swift
body: <p>I am trying to load <code>JSON</code> Data into <code>CoreData</code>. Here, I used JSON <code>structure</code> for decodable and after that I am trying to fetch the <code>decodable</code> Data into CoreData. I don’t know how to use decodable with <code>iterate</code> the data and fetching into coredata. Below code I tried, but not getting proper result. Please help me to resolve this.</p>
  
  <p><strong>My JSON</strong></p>
  
  <pre><code>{
      "status": true,
      "data": [
          {
              "id": "20",
              "name": "ar1"
          },
          {
              "id": "21",
              "name": "ar2"
          },
          {
              "id": "22",
              "name": "ar3"
          }
      ]
  }
  </code></pre>
  
  <p><strong>My Structure For JSON</strong></p>
  
  <pre><code>struct userList: Codable {
      let status: Bool
      let data: [userData]
  }
  
  struct userData: Codable {
      let id, name: String
  }
  </code></pre>
  
  <p><strong>My Coredata Entity</strong></p>
  
  <pre><code>var users = [User]() // User is my entity name
  </code></pre>
  
  <p><strong>JSON Codable and Try  ing to Load Into CoreData</strong></p>
  
  <pre><code>do {
         let result = try JSONDecoder().decode(userList.self, from:data) // here I am dong mistakes.
         let status = result.status
         if status == true {
             let newPerson = User(context: self.context)
             newPerson.id = "" // Cant able to get values
             newPerson.name = ""
             self.users.append(newPerson)
  
              DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                  self.tableView.reloadData()
               }
              } else {
                 print(error)
               }
  
      } catch {
           print(error)
  }
  </code></pre>