How to access nested arrays in a smart value automation?
Asked by Darren Nicas:

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"


Answers:
author: Bill Sheboy
body: Hi [@Darren Nicas](/t5/user/viewprofilepage/user-id/5294405)

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

comments: author: Darren Nicas
body: Hi [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313)

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.


author: Bill Sheboy
body: 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}}


author: Darren Nicas
body: 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.


author: Kalyan Sattaluri
body: 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}} {{/}}{{/}}{{/}}{{/}}

comments: author: Darren Nicas
body: Hi [@Kalyan Sattaluri](/t5/user/viewprofilepage/user-id/5312306) 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.