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

  • Hi Brinda,

    You should be able to extract the information like this:

    /* change the result into JSON and get the relevant result body*/
    local!lat: a!fromJson({your variable}.body.results.geometry.location.lat),
    local!lng: a!fromJson({your variable}response.body.results.geometry.location.lng)
  • 0
    Certified Associate Developer
    in reply to jeromew

    Hi Jeromew,

    In the integration I am using  "GET" method and using parameters to pass address and key.  I am not using body in my inputs.

    But I am getting the result in dictionary.  

    When I try to change the result into JSON by using the code suggested by you, it is throwing an error as follows:

    Expression evaluation error at function a!fromJson [line 9]: Error evaluating function fn!fromjson_appian_internal : The jsonText parameter must not be null or empty.

    Kindly let me know why I am getting the error and how can get the latitude and longitude?

     

     

     

  • Similar to what was described above, doing this should work:

    = with(
      local!result: rule!MY_INTEGRATION_RULE(),
      local!json: a!fromJson(local!result.result.body),
      local!lat: local!json.results.geometry.location.lat,
      local!lng: local!json.results.geometry.location.lng
    )

    Double check the json response. Start by just printing the response and then go down a level to see if it still returns valid responses. Continue until you get all the way to the lat an lon.

     

    You may want to check how you are passing in the rule input into your integration object.

     

    Ensure that it looks like

    "https://maps.googleapis.com/maps/api/geocode/json?address=" & ri!address + "&key=<MY_KEY>

  • 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 benjamins
    Thank you Benjamin. I created an expression rule and called it in the interface where I am giving address as input. Now I am able to get the latitude and and longitude for the given address.
  • 0
    Certified Associate Developer
    in reply to malcolm.ross
    Thank you Malcom. I will try as suggested by you.