Legacy Dev Forum Posts

 View Only

Sign Up

Help on Response Syntax for Data Action

  • 1.  Help on Response Syntax for Data Action

    Posted 06-05-2025 18:40

    N25 | 2017-10-12 05:48:35 UTC | #1

    I've been trying to create a data action that would work with this public webservice for US zip codes (https://api.zippopotam.us/us/) but I can't seem to get the heirarchial output from it.

    I read up on response configuration on this page (https://help.mypurecloud.com/articles/response-configuration-data-actions/) and applied it as close to the examples as I can but I can't save the data action due to a "The request could not be understood by the server due to malformed syntax." error.

    Here is the response configuration that I made: { "translationMap": { "postcodeValue": "$.post code", "countryValue": "$.country", "countryabbrevValue": "$.country abbreviation", "placesValue": "$.places" }, "successTemplate": { "post code": "${postcodeValue}", "country": "$(countryValue}", "country abbreviation": "${countryabbrevValue}", "places": "${placesValue}" } }

    Here is the raw output from the webservice if you enter a zip code of 90210: { "post code": "90210", "country": "United States", "country abbreviation": "US", "places": [ { "place name": "Beverly Hills", "longitude": "-118.4065", "state": "California", "state abbreviation": "CA", "latitude": "34.0901" } ] }

    Is my response configuration coded incorrectly?


    ChrisDibble | 2017-10-12 20:49:30 UTC | #2

    Hi! I've got two solutions for you depending on what end goal you'd like to accomplish. I noticed that the successTemplate you created reconstructs the full response that the zippopotam service returns. If you want to return the entire response from zippopotam, we can simplify your response configuration significantly because that's what the data action will do by default. I'll go through that solution as well as correct the configuration you provided in case this was a test action before moving on to a more complex web service call.


    If you are wanting to return the full response You don't need to use the translation map or success template at all. Your response configuration section would look like this

    { "translationMap": {}, "successTemplate": "${rawResult}" }


    If you need to restructure or reorganize the data from the response This is the more complicated scenario. If you need to manipulate the data being returned (reorder, add boilerplate, only return a subset, etc) you will need to use the translationMap and successTemplate. I corrected the configuration you provided and it looks like this

    { "translationMap": { "postcodeValue": "$.['post code']", "countryValue": "$.country", "countryabbrevValue": "$.['country abbreviation']", "placesValue": "$.places" }, "successTemplate": "{\"post code\": ${postcodeValue}, \"country\": ${countryValue}, \"country abbreviation\": ${countryabbrevValue}, \"places\": ${placesValue}} " }

    There were four small things I needed to change to get it working.

    • The response from zippopotam has keys containing spaces. To reference those in JSONPath, you have to surround the field name in brackets and single quotes ['fieldName']. This information was missing from the documentation. I'll see that it gets updated.
    • The successTemplate needs to be a string value. Once changing it to a string, I needed to add escape characters before the quotes
    • The countryValue variable had a ( on one side and a } on the other. I corrected the ( to be {
    • When using Velocity to inject values into the successTemplate, there is no need to surround the variables in quotes. If the value you are injecting is a string the quotes will already be there and if the thing you are injecting is a json object, quotes would be invalid.

    Hopefully these examples and explanations help. Feel free to post back if anything gives you trouble and we'll do what we can to help.

    -Chris


    N25 | 2017-10-12 22:59:12 UTC | #3

    Thanks a lot Chris! I tried the first, simpler response configuration before but I can only extract the values from the post code, country and country abbreviation output and not from the places output which made me think to manipulate those output from the response configuration in order to flatten them. Basically, I need the output to be in this format in order to be usable in Architect:

    { "country": "United States", "places.state abbreviation": [ "CA" ], "places.place name": [ "Beverly Hills" ], "places.state": [ "California" ], "places.longitude": [ "-118.4065" ], "places.latitude": [ "34.0901" ], "country abbreviation": "US", "post code": "90210" }

    I'm going to try the 2nd one and give you feedback on that one.

    Again, thanks for the very informative reply!


    ChrisDibble | 2017-10-12 23:31:53 UTC | #4

    When Architect executes the data action, the service will attempt to flatten the data action response for you. To see this, in the Actions Admin UI, on the "Test" tab, be sure to select the "Flatten output" checkbox to simulate that. <img src="//inin-prod-use1-developerforum.s3.amazonaws.com/original/1X/5e2ddc84cfb07a879e305de7c69106ef444d6620.jpeg" width="478" height="500">

    The service will also automatically flatten the output contract at execution time as well, so you can generate a schema straight from the webservice response without having to flatten it yourself.

    -Chris


    N25 | 2017-10-13 01:28:24 UTC | #5

    Didn't realize that the flattening happens on the fly, it's not really that clear on the documentations as before when I use the bridge actions I had to check the flatten output checkbox. If that is the case as well I just need to understand as to why I can't extract the hierarchial data from "places":

    "place name": "Beverly Hills", "longitude": "-118.4065", "state": "California", "state abbreviation": "CA", "latitude": "34.0901"

    Thanks again Chris! Will try out these later.

    Edit: BTW, could you please share the schema that you did for this action as well? Would just like to double-check on mine, thanks!


    ChrisDibble | 2017-10-13 12:28:46 UTC | #6

    Sure, here is the output schema I used. Let me know if you are still having trouble getting the data from the places array. If so, please post the result of testing the action.

    -Chris

    { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": { "country": { "type": "string" }, "country abbreviation": { "type": "string" }, "post code": { "type": "string" }, "places": { "items": { "properties": { "latitude": { "type": "string" }, "longitude": { "type": "string" }, "place name": { "type": "string" }, "state": { "type": "string" }, "state abbreviation": { "type": "string" } }, "type": "object" }, "type": "array" } } }


    N25 | 2017-10-16 02:36:41 UTC | #7

    Thanks a lot Chris! It looks like I didn't define places as an array in my schema, after doing your method and just getting the raw result I was finally able to manipulate the data from places in Architect :slight_smile:

    Just to clarify, for any responses that comprise of several data in it should I always define it as an array? For example:

    "outcome": { "networkstatus": "approvedbynetwork", "reason": null, "risklevel": "normal", "seller_message": "Payment complete.", "type": "authorized" },

    would mean outcome should be defined as an array in the output contract?


    ChrisDibble | 2017-10-16 13:28:59 UTC | #8

    The output schema should be based on the data returned from the remote web service. In almost all cases, you'll only define an array in your schema if the remote web service returns an array. In your case, the output object never uses an array so you shouldnt include one in the schema. In the zippopotam example, "places" is a field that already contains an array, so you'd need to capture that in your schema.

    -Chris


    N25 | 2017-10-17 03:16:27 UTC | #9

    Thanks a lot Chris for the very informative replies! Pardon me as I'm still trying to figure out this part of Purecloud, I'll be taking note of your tips as well :slight_smile:


    system | 2017-11-17 03:16:30 UTC | #10

    This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.


    This post was migrated from the old Developer Forum.

    ref: 1936