I have a problem with an external webservice (api rest method that delivers information about car workshops) when returning the workshop id value I see that it is not consistent, the response from warehouseID in some cases is an integer example "505" and in other cases it is a string example "H55". when generating the contract variable both if I leave it as a string and as an integer, I get an error, how can I format the response (warehouseID) to a string for each object and store in an array contract variable?, I have tried the answer of "sucesstemplate" but without success.
Could you provide an export of your action and examples of responses that have different warehouseID types? Feel free to replace anything sensitive with xxxx, but do not change the shape of the data.
hi Jason..
i have to recreate the JSON Response , but the example is very similar to the actual answer. (Webserver with problems)
JSON Rsponse.
{
"workshops":[{
"name" : "Test",
"warehouseId" : 100
},
{
"name": "Test2",
"warehouseId" : 200
},
{
"name": "Test3",
"warehouseId" : "H10"
},
{
"name" : "Test4",
"warehouseId" : "H20"
},
{
"name": "Test5",
"warehouseId" : 300
},
{
"name": "Test6",
"warehouseId" : 400
}
]
}
data action
translation
{
"translationMap": {
"name": "$.workshops[*].name",
"cantidad": "$.workshops.length()",
"warehouseId": "$.workshops[*].warehouseId"
},
"translationMapDefaults": {
"name": "[]",
"cantidad": "0",
"warehouseId": "[]"
},
"successTemplate": "{ \n \n \"name\":${name} \n, \n \"warehouseId\":\"${warehouseId}\"\n, \n \"cantidad\":${cantidad} \n }"
}
Regards.
Assuming that you can't get the endpoint to return a single type of warehouseId, here is an approach that works based on your example:
{
"translationMap": {
"name": "$.workshops[*].name",
"cantidad": "$.workshops.length()",
"warehouseId": "$.workshops[*].warehouseId"
},
"translationMapDefaults": {
"name": "[]",
"cantidad": "0",
"warehouseId": "[ ]"
},
"successTemplate": "{ \n \n \"name\":${name} \n, \n \"warehouseId\":$warehouseId.replace(\", \", \"$esc.quote, $esc.quote\").replace(\"[ \", \"[ $esc.quote\").replace(\" ]\", \"$esc.quote ]\").replace(\"$esc.quote$esc.quote\", \"$esc.quote\").replace(\"[ $esc.quote]\", \"[]\")\n, \n \"cantidad\":${cantidad} \n }"
}
The first three string replacements put quotes around every entry in the array, even entries that already have quotes.
The fourth replace cleans up entries that were already quoted by replacing "" with "
The final string replace handles the case of not getting any warehouseIds back from the endpoint.
Ugly, but might get you working.
--Jason
Thanks Jason, the transformation works perfectly.
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.