Hey community,
I am trying to get one specific element in an array returned in a {{webResponse}} in Automation. I have found the method .get() which requires the number of the position of the element and it works fine.
The position is different by the runs which means that I need this to be dynamic. My first try was to use a smart value as the position, like:
{{webResponse.body.data.get({{myPosition}})}}
but the get-function does not support smart values like this.
My example is this: I have the following webResponse returned in my automation:
{
"data":
[
{
"id": "A",
"name": "foo"
},
{
"id": "B",
"name": "bar"
},
{
"id": "C",
"name": "baz"
}
]
}
In my automation I now want to get the name attribute for the element having id == "B", i.e. "bar"
Is there a way to get that specific element from the webResponse using any filtering or parsing so that I can use the element in later actions such as:
{{webResponse.body.data."id==B".name}} => "bar"
Looking forward to hear back from you!
Cheers,
// Svante
Hi @Svante Gustafsson Björkegren
Short answer: the inline, list function get() requires a number, and it cannot take a Created Variable directly. There are several workarounds, assuming your myPosition is a Created Variable.
Force the correct number typing for the get() function. There are two versions of the get() function: one for inline, list iteration and one for lookup tables. The list version suffers a known limitation where many functions cannot use a Created Variable as a number parameter, even when converted using asNumber. The problem seems to be the value type stays "text" in the function call, even though it is converted to a number. (Aside: the newer get() function for lookup tables does not have this limitation.)
As I described in this article, there is a workaround to force the type as a number by ensuring the first thing the get() "sees" is a number. This may be done by adding another, empty created variable, such as with:
{{webResponse.body.data.get(varNull.length().plus(myPosition.asNumber))}}
How that works is:
I recommend trying this approach before the other two.
Branch and test with a condition (as @Tomislav Tobijas suggested with the link to that article). This approach may not help if the parallel processing / asynchronous behavior of branches does not fit with your rule structure.
Dynamic list searching using a regular expression. This approach is likely not needed with the number get() you are trying, and is more relevant for complex searches of list data with text. Please see this article I wrote to learn more about this technique.
Kind regards,
Bill
Hey @Svante Gustafsson Björkegren ,
You might want to check this resource: JSON Queries in Jira Automation
Based on the article above, maybe something like this would work:
Use an "Advanced branching" action on {{webResponse.body.data}}
Inside the branch, add a condition: If {{item.id}} equals "B"
.
In the "Then" branch, use {{item.name}}
(which will be "bar"
in your example).
I believe we constructed something like this a while back, but I've lost track of that automation to check it out 😅
Bonus, here's another feature request that might be relevant to the use case: AUTO-1682: Add support for find() and filter() functions in automation smart values
Cheers,
Tobi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.