javier.delolmo | 2021-02-09 14:41:19 UTC | #1
I have an API service, which returns a JSON in the following format:
{
"name":"fake name",
"products": [
{
"id": "aaa",
"product": "bbb",
"descripcion": "ccc"
},
{
"id": "ddd",
"product": "eee"
},
{
"id": "fff",
"product": "ggg",
"description": "hhh"
}
]
}
Some fields such as the description of the products is not mandatory, but in Architect I have to tell the client the following phrase "the product with id XXX is called YYY and its description is ZZZ" or "the product with id XXX is called YYY" if the product has no description. In other data actions that I have configured, what I do is create as a result of the data action 3 variables of type array (ids, products and descriptions) and in the Architect flow I use a loop to go through the array, however, with the example above it would have 2 arrays (ids and products) of 3 items and 1 array (descriptions) of 2 items and when it was in the second round of the loop it would tell the client "the product with id ddd is called eee and its description is hhh". How could I create in a data actions arrays of the same length and that the array "descriptions" of the example above would be like this ["ccc", "", "hhh"] ???
Thanks.
Jerome.Saint-Marc | 2021-02-10 14:15:41 UTC | #2
Hello,
I have defined a Data Action with 2 arrays of objects (withDescription, noDescription), in the Output Contract. If you invoke this Data Action from Architect, they will be automatically flattened and you will have an array variable for each attribute of each objects.
In the Response configuration, I am using a JSONPath expression to check if the "description" exists or not. $.products[?(@.description)] will return products objects with a description attribute $.products[?(!@.description)] will return products objects with no description attribute
My Output contract:
{ "type": "object", "properties": { "withDescription": { "type": "array", "items": { "title": "Item 1", "type": "object", "properties": { "id": { "type": "string" }, "product": { "type": "string" }, "description": { "type": "string" } }, "additionalProperties": true } }, "noDescription": { "type": "array", "items": { "title": "Item 1", "type": "object", "properties": { "id": { "type": "string" }, "product": { "type": "string" } }, "additionalProperties": true } } }, "additionalProperties": true }
My Response configuration:
{ "translationMap": { "withDescription": "$.products[?(@.description)]", "noDescription": "$.products[?(!@.description)]" }, "translationMapDefaults": { "withDescription": "[]", "noDescription": "[]" }, "successTemplate": "{\n \"withDescription\": ${withDescription}, \"noDescription\": ${noDescription} \n }" }
You will also need to do 2 more things in your Architect Flow - test if if the arrays are empty or not (I mean at least one array from withDescription, one array from noDescription) as you could have 0 products with description and/or 0 products with no description.
Let's say that withDescription.id is assigned to State.withDescriptionId variable, and noDescription.id is assigned to State.noDescriptionId variable. You can use Decision blocks to evaluate if the variable/array is empty or not. Ex: Decision with Expression IsNotSetOrEmpty(State.noDescriptionId) - will return true if the array is empty, and false if the array is not empty.
Regards,
javier.delolmo | 2021-02-11 16:19:10 UTC | #3
Thank you very much Jerome, you are a pro! It is not the first time that one of your post in the developers forum saves my life :)
Regards.
Jerome.Saint-Marc | 2021-02-11 17:18:40 UTC | #4
Glad it helped. That's the most important :slight_smile:
Regards,
system | 2021-03-14 17:18:45 UTC | #5
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: 9944