Lloyd | 2021-07-19 07:47:09 UTC | #1
I've have an API call that returns an array of data, of which I only want the data from a certain type of the response.
Example Response:
[ { "category": "DEBT", "description": "top", "amount": "38.39" }, { "category": "INVO", "description": "not top", "amount": "2941.92" }, { "category": "PAYMT", "description": "not bottom", "amount": "-4.84" }, { "category": "", "description": "bottom", "amount": "0.00" } ]
I only want the data from the object with Category of INVO
For my TranslationMap I have used:
$[?(@.category == 'INVO')].amount
This returns me an array of a single value
[ "2941.92" ]
But when I try to assign it this in my TranslationMap I get an error
JSON failed schema validation for the following reasons: Schema: # @/properties/amount. Error location: /amount. instance type (array) does not match any allowed primitive type (allowed: [\"string\"])
My question is how do I fix my translationMap to return the value for amount and not an array with it in
Thanks
Jerome.Saint-Marc | 2021-07-19 08:40:53 UTC | #2
Hello,
I have defined *myAmount* as a string, in my Output Contract.
And this is my Response Configuration:
{ "translationMap": { "arrayAmount": "$[?(@.category == 'INVO')].amount" }, "translationMapDefaults": {}, "successTemplate": "{\"myAmount\": ${successTemplateUtils.firstFromArray(\"${arrayAmount}\",\"$esc.quote$esc.quote\")}}" }
successTemplateUtils.firstFromArray macro gives me the first element of the array (first parameter). The second parameter of that macro is the default value if the array is empty (using \$esc.quote\$esc.quote to send back an empty string)
Regards,
Lloyd | 2021-07-19 19:39:15 UTC | #3
Thank you Jerome - This works perfectly!
system | 2021-08-19 19:39:19 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: 11505