title: Convert swift object (with nested objects) to JSON data in a pure Swift 3 way
body: <p>I am trying to convert a swift object to JSON, I have seen these SO questions <a href="https://stackoverflow.com/questions/32119177/converting-object-to-json-string-for-post-request">1</a> &amp; <a href="https://stackoverflow.com/questions/29599005/how-to-convert-or-parse-swift-objects-to-json">2</a>, but I could not apply it to my code.</p>
  
  <p>I have a swift object of type <code>[String : DailyTimes]</code> which I would like to convert it back to JSON data in a pure Swift 3 way, without any libraries.</p>
  
  <p>Below is my custom class:</p>
  
  <pre><code>class AvailabilityTimes:{
  
      struct Times{
          var startTime : String?
          var endTime   : String?
  
  
      }
  
      struct DailyTimes{
          let weekday : String
          var available : Bool
          var times = [Times]()
          mutating func update(times: [Times]){
              self.times = times
          }
  
      }
  }
  </code></pre>
  
  <p>The converted JSON data (from Swift object) would look something like this:</p>
  
  <pre><code>[
       "Monday": [ "weekday" : "Monday",
                   "available" : true,
                   "times": [
                       ["startTime": "9:00 AM", "endTime": "1:30 PM" ],
                       ["startTime": "2:30 PM", "endTime": "6:00 PM" ],
                       ["startTime": "7:30 PM", "endTime": "9:00 PM" ]
                   ]
       ],
       "Tuesday": [ "weekday" : "Tuesday",
                   "available" : true,
                   "times": [
                       ["startTime": "9:00 AM", "endTime": "6:00 PM" ]
                   ]
                   ]
       ]
  </code></pre>
  
  <p><strong>My unsuccessful attempt to convert <code>[String : DailyTimes]</code> to JSON data</strong></p>
  
  <p>Step 1: Added convertToDictionary function in both structs.</p>
  
  <pre><code>class AvailabilityTimes:{
  
      struct Times{
          var startTime : String?
          var endTime   : String?
  
          func convertToDictionary() -&gt; Dictionary&lt;String, Any&gt; {
              return [
                  "startTime" : self.startTime,
                  "endTime"   : self.endTime
              ]
          }
      }
  
      struct DailyTimes{
          let weekday : String
          var available : Bool
          var times = [Times]()
          mutating func update(times: [Times]){
              self.times = times
          }
  
          func convertToDictionary() -&gt; Dictionary&lt;String, Any&gt; {
              return [
                  "weekday"   : self.weekday,
                  "available" : self.available,
                  "times"     : self.times
              ]
          }
  
      }
  } 
  </code></pre>
  
  <p>This is where I am unsuccessfully trying to convert to JSON data.</p>
  
  <p>Step 2: Function that converts to JSON data</p>
  
  <pre><code>func convertTimesObjectToJSON(timesObject: [String : DailyTimes]){
  
                  for (key, value) in timesObject{
  
                      let dictionary =  value.convertToDictionary
  
                      print("dictionary", dictionary)
                      do{
                          let theJSONData = try JSONSerialization.data(withJSONObject: dictionary, options: .prettyPrinted)
                      } catch let error{
                          print("error: \(error)")
                      }
  
  
  
                  }
  
              }
  </code></pre>