faisyou | 2021-09-19 18:23:13 UTC | #1
Hi, I am making a conversations aggregate API query - to get the number of interactions per day for a customer.
InteractionsInInterval-20210919222018.custom.json|attachment (2.1 KB) Following is the sample returned JSON from the API
{ "results": [ { "group": { "mediaType": "voice" }, "data": [ { "interval": "2021-09-18T21:19:00.000Z/2021-09-19T21:19:00.000Z", "metrics": [ { "metric": "nConnected", "stats": { "count": 4 } } ] } ] } ] }
=> I use the following JSONPath expression to extract the nConnected stat $.results[0].data[0].metrics[?(@.metric=="nConnected")].stats.count
=> Following is my Respone Configuration { "translationMap": { "nConnected": "$.results[0].data[0].metrics[?(@.metric==\"nConnected\")].stats.count", "nOffered": "$.results[0].data[0].metrics[?(@.metric==\"nOffered\")].stats.count" }, "translationMapDefaults": { "nConnected": "0", "nOffered": "0" }, "successTemplate": "{\"nConnected\":${nConnected}},\"nOffered\":${nOffered}}" }
The JSONPath works in http://jsonpath.herokuapp.com/ - but in Data action - I see the following error => { "message": "JSON failed schema validation for the following reasons: Schema: # @/properties/nConnected. Error location: /nConnected. instance type (array) does not match any allowed primitive type (allowed: [\"integer\"])", "code": "invalid.schema", "status": 400, "messageParams": {}, "contextId": "d8245512-d5f3-409c-ab5f-8802f7131296", "details": [ { "errorCode": "ACTION.PROCESSING" } ], "errors": [] } => Basically - the JSONPath expression returns an array of Integer - but I dont know how to extract an integer from a filtered array And the obvious approach of adding an array deference operation after the filter doesnt work (well it returns an error even in the http://jsonpath.herokuapp.com/)
$.results[0].data[0].metrics[?(@.metric==\"nConnected\")][0].stats.count
does anyone how to solve this problem.
Jerome.Saint-Marc | 2021-09-21 02:30:09 UTC | #2
Hello,
You can use the following Response configuration:
{ "translationMap": { "nConnectedArray": "$.results[0].data[0].metrics[?(@.metric==\"nConnected\")].stats.count", "nOfferedArray": "$.results[0].data[0].metrics[?(@.metric==\"nOffered\")].stats.count" }, "translationMapDefaults": { "nConnectedArray": "[]", "nOfferedArray": "[]" }, "successTemplate": "{\"nConnected\":${successTemplateUtils.firstFromArray(\"${nConnectedArray}\",\"0\")},\"nOffered\":${successTemplateUtils.firstFromArray(\"${nOfferedArray}\",\"0\")}}" }
The result of the JSONPath expression is an array.
You can use the successTemplateUtils.firstFromArray macro to retrieve the first element of this array. This macro can take a second parameter (optional) to define a default value when the array is empty. The JSONPath expression will return an empty array in some cases - e.g. if there is no nConnected metric, ...
The translationMapDefaults will only be used/triggered if the response to your Platform API request does not contain some attribute names that you are using in your translationMap (e.g. results, data, metrics). I have defined empty arrays in the translationMapDefaults (nConnectedArray, nOfferedArray), so that it will also use the default value (second parameter) from the successTemplateUtils.firstFromArray macro in this case.
Regards,
faisyou | 2021-09-21 02:31:29 UTC | #3
Thanks for your assistance Jerome it solved my issue InteractionsInInterval-2021092162939.custom.json|attachment (2.6 KB)
I am also attaching the final Data Action - in case it helps someone else
system | 2021-10-22 02:32:21 UTC | #4
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: 12053