Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to access nested arrays in a smart value automation?

Darren Nicas January 31, 2024

I want to get the latest version change record from the jira cloud system audit log to get the old version name and the new version name for further automation for a 1-way sync of release versions (create, update, delete) to another project.

 

When I use a web request block in the automation to get the audit log record I'm looking for it looks like this:

{
"offset": 0,
"limit": 1,
"total": 1286,
"records": [
{
"id": 18896,
"summary": "Project version updated",
"remoteAddress": "000.000.000.000",
"authorKey": "0a",
"authorAccountId": "0a",
"created": "2024-01-31T07:58:21.296+0000",
"category": "projects",
"eventSource": "",
"objectItem": {
"id": "10341",
"name": "C-0.0.0",
"typeName": "VERSION"
},
"changedValues": [
{
"fieldName": "Name",
"changedFrom": "C-0.0.3",
"changedTo": "C-0.0.0"
}
],
"associatedItems": [
{
"id": "10014",
"name": "Sandbox CONTXT",
"typeName": "PROJECT"
}
]
}
]
}

 

The values I'm trying to access, I would expect to access by:

  1. `{webResponse.body.records(0).changedValues(0).changedFrom}}
  2. {{webResponse.body.records(0).changedValues(0).changedTo}}

However these produce no value when logging them in automation.

 

I am able to get a single value array using the following, but this doesn't help me as I still can't access the single value in the array as a string:

  1. {{webResponse.body.records.changedValues.changedFrom}}
  2. {{webResponse.body.records.changedValues.changedTo}}

I get values like this:

  1. [C-0.0.3]
  2. [C-0.0.0]

Then trying to access these values something like set it to a smart value, {{value.get(1)}} returns nothing when logging it with the automation.

 

How can I access the values as strings like the following?

  1. {{changedFrom}}="C-0.0.3"
  2. {{changedTo}}="C-0.0.0"

2 answers

1 accepted

0 votes
Answer accepted
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 31, 2024

Hi @Darren Nicas 

I am currently using the Free license of Jira Cloud, and so have no access to the audit logs to test my suggestions...

That web response appears to contain an array, and not a list of values.  This would explain why the get() function is not working as you expect.

You could try grabbing the records and then using a match() with a regular expression on that, progressively parsing to get the values.  https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/#match--

Kind regards,
Bill

Darren Nicas January 31, 2024

Hi @Bill Sheboy 

 

After unsuccessfully trying with match per your suggestion I went back to get() again. The syntax that ended up working for me to access the fields were as follows:

  1. versionChangedTo: {{webResponse.body.records.changedValues.get(0).changedTo}}
  2. versionChangedFrom: {{webResponse.body.records.changedValues.get(0).changedFrom}}

With those pulling directly from the webrequest (example JSON in my first post) it appears to be working.

 

It is bizarre to me that the correct json syntax doesn't work though. I am accessing an array of objects nested inside another array of objects so I'd expect it to be {{webResponse.body.records.get(0).changedValues.get(0).changedTo}} but that doesn't work.

get(0) only seems to be needed on the nested array for some reason, even though they are both arrays of a single object.

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 1, 2024

That is curious...

Have you tried any tests with multiple items in both records and changedValues to confirm the correct item is returned with only one get() call at the end?

In my experience, other automation rule, smart values which return lists AND have returned only one item seem to alias (or collapse data structures) such that the child attributes work as if there was no list.  For example:

{{issue.sprint.name}} versus {{issue.sprint.first.name}} versus {{issue.sprint.get(0).name}}

Like Darren Nicas likes this
Darren Nicas February 2, 2024

I haven't since in this case I didn't need more than one item in the array since I searched for only the latest change. Could be a risk though seeing the rule I put this into can take up to 19 seconds to complete running and the delays in jira automation executions I've been seeing it might be smart to expand the audit log search to grab more results and look for only the one I need. Will have to come back to this when time permits, thanks for the recommendation.

Like Bill Sheboy likes this
0 votes
Kalyan Sattaluri
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 31, 2024

Can you share the end point you are using, I never used it and so dont know response structure... but curious, can you log below smart value to see if anything is getting returned.

 

{{#webhookResponse.body.records}}{{#changedValues}}{{#if(equals(fieldName,"Name"))}}{{#first}}{{changedFrom}} {{/}}{{/}}{{/}}{{/}}

Darren Nicas January 31, 2024

Hi @Kalyan Sattaluri you can find the endpoint here: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-audit-records/#api-rest-api-3-auditing-record-get

 

Any changes made in the jira site will be logged here, so you can capture version change events since atlassian doesn't provide any smartvalue for these.

 

Thanks for the suggestion, I can never get that syntax down right. I actually solved this a different way.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events