title: Save complex JSON to Core Data in Swift
body: <p>I want to save the JSON Result from web service to Core data, following is the code for JSON parsing.</p>
  
  <pre><code>if let jsonResult = try JSONSerialization.jsonObject(with: JSONData!, options: [.mutableContainers]) as? [String: AnyObject]
  </code></pre>
  
  <p>If i prints jsonResult, following is the output</p>
  
  <pre><code>["Code": 00, "Desc": success, "iinData": &lt;__NSArrayM 0x1c02531a0&gt;
  {
    name = “AAA”;
    iin = 123456;
    isOn = 0;
  },
  {
    name = "Allahabad Bank";
    iin = 608112;
    isOn = 0;
  },
  )
  </code></pre>
  
  <p>I can able to insert <strong>Code, Desc</strong> into Entity1, but how to insert innData into Entity2 . </p>
  
  <p><a href="https://i.stack.imgur.com/vbWyy.png" rel="nofollow noreferrer">Entity1 Structure</a> <a href="https://i.stack.imgur.com/FUW1b.png" rel="nofollow noreferrer">Entity2 Structure</a></p>
  
  <p>Following is the code for inserting JSON result into core data</p>
  
  <pre><code>func createEntity1From(dictionary: [String: AnyObject]) -&gt; NSManagedObject? {
      let context = CoreDataStack.sharedInstance.persistentContainer.viewContext
      if let Entity1 = NSEntityDescription.insertNewObject(forEntityName: “ParamDownload”, into: context) as? ParamDownload {
          Entity1.Code= dictionary[“name”] as? String
          Entity1.desc = dictionary[“desc”] as? String
          return Entity1
      }
  }
  </code></pre>