How to convert dictionary to CDT?

Certified Associate Developer

Hi all,

I am using an Appian integration with Google Maps in which I am passing an address to get the latitude and longitude of that address.  I am getting the result in dictionary format as follows:

 

Value

Dictionary success: true result: Dictionary headers: Dictionary Content-Type: "application/json; charset=UTF-8" Date: "Sat, 06 Jan 2018 04:41:42 GMT" Expires: "Sun, 07 Jan 2018 04:41:42 GMT" Cache-Control: "public, max-age=86400" Vary: "Accept-Language" Access-Control-Allow-Origin: "*" Server: "mafe" X-XSS-Protection: "1; mode=block" X-Frame-Options: "SAMEORIGIN" statusLine: "HTTP/1.1 200 OK" body: "{ "results" : [ { "address_components" : [ { "long_name" : "46", "short_name" : "46", "types" : [ "street_number" ] }, { "long_name" : "2nd Street", "short_name" : "2nd St", "types" : [ "route" ] }, { "long_name" : "Said Nagar", "short_name" : "Said Nagar", "types" : [ "neighborhood", "political" ] }, { "long_name" : "Balaji Nagar", "short_name" : "Balaji Nagar", "types" : [ "political", "sublocality", "sublocality_level_2" ] }, { "long_name" : "Virugambakkam", "short_name" : "Virugambakkam", "types" : [ "political", "sublocality", "sublocality_level_1" ] }, { "long_name" : "Chennai", "short_name" : "Chennai", "types" : [ "locality", "political" ] }, { "long_name" : "Tiruvallur", "short_name" : "Tiruvallur", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Tamil Nadu", "short_name" : "TN", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "India", "short_name" : "IN", "types" : [ "country", "political" ] }, { "long_name" : "600092", "short_name" : "600092", "types" : [ "postal_code" ] } ], "formatted_address" : "46, 2nd St, Said Nagar, Balaji Nagar, Virugambakkam, Chennai, Tamil Nadu 600092, India", "geometry" : { "location" : { "lat" : 13.0556007, "lng" : 80.1981811 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 13.0569496802915, "lng" : 80.19953008029151 }, "southwest" : { "lat" : 13.0542517197085, "lng" : 80.1968321197085 } } }, "place_id" : "ChIJgxfuDshmUjoR8SPovCPMInM", "types" : [ "street_address" ] } ], "status" : "OK" } " contentType: "application/json; charset=UTF-8" statusCode: 200 error: null (Null)

 

How can I extract only the latitude and longitude from the above format? Should I convert this dictionary format to CDT format? How can I achieve that?

 

Thanks in advance

 

  Discussion posts and replies are publicly visible

Parents
  • It would first be most efficient to run the fromJson once for your entire JSON string instead of calling it multiple times..

    Like this

    load(
    local!googleMapsValue: a!fromJson( {your JSON string} ),
    ................

    Then declare a local variable of the desired CDT type using type! to create a new value of the desired CDT type

    'type!{urn:com:appian:types:{NAMESPACE}}{CDT NAME}'()

    If you are using 17.4, then you will get an autosuggest when you type type! to pick your desired CDT.

    Once you have your value of type declared, then start filling in the values. like

    load(
    local!googleMapsValue: a!fromJson( {your JSON string} ),
    local!CDTwithLatLng: 'type!{urn:com:appian:types:{NAMESPACE}}{CDT NAME}'(

    lat: local!googleMapsValue.results.geometry.location.lat
    lng: local!googleMapsValue.results.geometry.location.lng
    ),

    local!CDTwithLatLng
    )


    So essentially what you are doing is first converting the JSON to a data dictionary.

    Then you are extracting the values you want using . notation and placing them in the desired CDT structure by declaring the variable to be of a certain type with type!

    As mentioned, it is most efficient to only convert the JSON string once to data dictionary rather call fromJSON multiple times.

  • 0
    Certified Associate Developer
    in reply to malcolm.ross
    Thank you Malcom. I will try as suggested by you.
Reply Children
No Data