Page 1 of 1

JSON Reader for Nested Arrays

Posted: Thu Sep 06, 2018 12:50 pm
by jan
Hi. I am trying to get the "County" name using Google's Geocoder API. The API returns a JSON object with nested arrays.

Code: Select all

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "102",
               "short_name" : "102",
               "types" : [ "subpremise" ]
            },
            {
               "long_name" : "2001",
               "short_name" : "2001",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Butterfield Road",
               "short_name" : "Butterfield Rd",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Downers Grove",
               "short_name" : "Downers Grove",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "York Township",
               "short_name" : "York Township",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "DuPage County",
               "short_name" : "Dupage County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Illinois",
               "short_name" : "IL",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "60515",
               "short_name" : "60515",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "2001 Butterfield Rd #102, Downers Grove, IL 60515, USA",
         "geometry" : {
            "location" : {
               "lat" : 41.8296848,
               "lng" : -88.0326829
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.8310337802915,
                  "lng" : -88.03133391970849
               },
               "southwest" : {
                  "lat" : 41.8283358197085,
                  "lng" : -88.03403188029151
               }
            }
         },
         "place_id" : "EjYxMDIsIDIwMDEgQnV0dGVyZmllbGQgUmQsIERvd25lcnMgR3JvdmUsIElMIDYwNTE1LCBVU0EiHxodChYKFAoSCVkkY18iUg6IEfeMNcMHCrIpEgMxMDI",
         "types" : [ "subpremise" ]
      }
   ],
   "status" : "OK"
}
To get the County, what I am trying to do is loop through address_components and get long_name temporarily. Then loop through types to check for the value "administrative_area_level_2" as that indicates that the long name previously saved is the County.

Code: Select all

#JsonReader.BeginObjectWithName( "results/1/address_components" )

For Each(#Object) In(#JsonReader.ReadObjectAtIndex( 1 ))
#L_Text := #Object.ItemAt<1>.AsString.AsNativeString

If Cond(#Object.ItemAt<3>.isArray)
#L_ArrayString := #Object.ItemAt<3>.AsString

#JsonReader2.SetSourceString String(#L_ArrayString)

For Each(#Object2) In(#JsonReader2.ReadObjectAtIndex( 1 ))
If Cond(#Object2.ItemAt<1>.AsString.AsNativeString.UpperCase = 'ADMINISTRATIVE_AREA_LEVEL_2')
#L_CountyFound := True
Leave
Else
Continue
Endif
Endfor

#JsonReader2.EndObject

Leave If(#L_CountyFound)
Endif
Endfor

#JsonReader.EndObject
When I try to debug this code though, the first For Each does not go through indicating that there were no entries found. What would be the correct way to do this using JsonReader?

Re: JSON Reader for Nested Arrays

Posted: Thu Sep 06, 2018 1:19 pm
by tsupartono

Code: Select all

Define_Com Class(#PRIM_DC.UnicodeString) Name(#County)

For Each(#Component) In(#Reader.ReadArrayWithPath( 'results/1/address_components' ))
   If (#Component.Item<'types'>.ItemAt<1>.AsString = 'administrative_area_level_2')
         #County := #Component.Item<'long_name'>.AsString 
         Leave
   Endif
Endfor

Re: JSON Reader for Nested Arrays

Posted: Thu Sep 06, 2018 2:19 pm
by jan
That did the trick. Thanks a lot.