Search code examples
swiftgoogle-mapsdirectionsdecodable

Swift - Decoding a Deeply Nested Dictionary


I am so close - but I am struggling with a very simple function to allow me to access a data point deeply nested in my JSON. The example I am using is on the Google directions API.

Sample JSON (from GMapsAPI):

{
  "geocoded_waypoints" : [
  {
     "geocoder_status" : "OK",
     "partial_match" : true,
     "place_id" : "ChIJ960bMolw44kRQcGOlOZQ-r8",
     "types" : [ "premise" ]
  },
  {
     "geocoder_status" : "OK",
     "partial_match" : true,
     "place_id" : "EiMxMTggU2FsZW0gU3QsIEJvc3RvbiwgTUEgMDIxMTMsIFVTQSIaEhgKFAoSCSvDfDSJcOOJEbQanF0WxROfEHY",
     "types" : [ "street_address" ]
  }
],
"routes" : [
  {
     "bounds" : {
        "northeast" : {
           "lat" : 42.3647252,
           "lng" : -71.0555085
        },
        "southwest" : {
           "lat" : 42.3644965,
           "lng" : -71.05552419999999
        }
     },
     "copyrights" : "Map data ©2018 Google",
     "legs" : [
        {
           "distance" : {
              "text" : "82 ft",
              "value" : 25
           },
           "duration" : {
              "text" : "1 min",
              "value" : 11
           },
           "end_address" : "118 Salem St, Boston, MA 02113, USA",
           "end_location" : {
              "lat" : 42.3647252,
              "lng" : -71.0555085
           },
           "start_address" : "115 Salem St, Boston, MA 02113, USA",
           "start_location" : {
              "lat" : 42.3644965,
              "lng" : -71.05552419999999
           },
           "steps" : [
              {
                 "distance" : {
                    "text" : "82 ft",
                    "value" : 25
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 11
                 },
                 "end_location" : {
                    "lat" : 42.3647252,
                    "lng" : -71.0555085
                 },
                 "html_instructions" : "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eSalem St\u003c/b\u003e toward \u003cb\u003eJerusalem Pl\u003c/b\u003e",
                 "polyline" : {
                    "points" : "ciqaG~_upLO?]A"
                 },
                 "start_location" : {
                    "lat" : 42.3644965,
                    "lng" : -71.05552419999999
                 },
                 "travel_mode" : "DRIVING"
              }
           ],
           "traffic_speed_entry" : [],
           "via_waypoint" : []
        }
     ],
     "overview_polyline" : {
        "points" : "ciqaG~_upLm@A"
     },
     "summary" : "Salem St",
     "warnings" : [],
     "waypoint_order" : []
  }
  ],
"status" : "OK"
}

Decodable Structure: To work with this, I am using Decodable. I have been able to access first level nested data (routes.summary), but I am struggling to get further down (for example: routes.legs.duration). My code structure is as follows:

struct Directions: Decodable {
    let status: String
    let routes: [Routes]

         enum CodingKeys :String, CodingKey {
              case status, routes
    }

struct Routes: Decodable {
    let summary: String
    let legs: [Legs]

         enum CodingKeys : String, CodingKey {
              case summary, legs
          }
}

struct Legs: Decodable {
    let duration: Duration

          enum CodingKeys : String, CodingKey {
          case duration
          }
    }

struct Duration: Decodable {
        let text: String    

            enum CodingKeys : String, CodingKey {
               case text
            }
        }

Implementation after URL set-up:

      URLSession.shared.dataTask(with: url) { (data, response, err) in
          guard let data = data else { return }
          do {   
             let directions = try
             JSONDecoder().decode(Directions.self, from: data)

        for item in directions.routes {
            self.stringoutput = item.summary          
       }

After all this, all I want to do is be able to access "text" in the JSON and return that value. The last line in the code is able to successfully return "summary" in the JSON; and I can print(directions) and the whole array/dictionary will return in the debug area, including "text". But I still can't figure out how to do:

x = directions.routes.legs.duration.text

to make x equal to "1 min"

Would be appreciative of anyone's help.

Edit: What ended up working is Vadian's struct keys below and the following for in loop:

                for item in directions.routes {
                    print(item.summary)
                    self.direct = item.summary
                    for items in item.legs {
                        self.stringoutput = items.duration.text
                        print(items.duration.text)
                        }

Cheers!


Solution

  • These structs don't decode all keys, but it's a starting point.

    If keys and struct members have the same name you don't need to specify CodingKeys

    struct Directions: Decodable {
        let status: String
        let routes: [Route]
    }
    
    struct Route: Decodable {
        let summary: String
        let legs: [Leg]
    }
    
    struct Leg: Decodable {
        let duration : TextValue
        let distance : TextValue
        let endAddress : String
        let endLocation : Location
        let startAddress : String
        let startLocation : Location
        let steps : [Step]
    }
    
    struct TextValue: Decodable {
        let text: String
        let value : Int
    }
    
    struct Location: Decodable {
        let lat, lng : Double
    }
    
    struct Step: Decodable {
        let duration : TextValue
        let distance : TextValue
        let endLocation : Location
        let startLocation : Location
        let htmlInstructions : String
        let travelMode : String
    }
    

    To decode the snake_cased keys properly you have to add the appropriate key decoding strategy

    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    

    To access the arrays ([]) you have to get an item by index

    step[0]
    

    or iterate the array with a loop

    for step in steps {}