title: Core Data - NSManagedObject (with relationships) to JSON
body: <p>I'm new to Core Data and I want to convert NSManagedObject (fetched from local database) to a JSON string. It's a simple object with relationships, as you can see above:</p>
  
  <p><a href="https://i.stack.imgur.com/Er9qf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Er9qf.png" alt="Entities"></a></p>
  
  <p>That's the code that I use for fetching:</p>
  
  <pre><code>func loadData() {
      //1
      guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
          return
      }
  
      let managedContext = appDelegate.persistentContainer.viewContext
  
      //2
      let fetchRequest = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: "Bill")
  
      //3
      do {
          let fetchedData = try managedContext.fetch(fetchRequest) as! [Bill]
      } catch let error as NSError {
          print("Could not fetch. \(error), \(error.userInfo)")
      }
  }
  </code></pre>
  
  <p>How can I convert fetchedData into a JSON string? I'd like to use Codable but i don't know if it's supported.</p>