How to parse through an json array where field_name is a separate object from value
Asked by Erin Quick-Laughlin:

I'm attempting to capture name-value pairs from an Incoming webhook event, and while most values in {{webhookData}} are a typical/expected JSON structure, the webhookData.audit.**changes** element is a an array, of two objects per element, where

first object is {"field_name":"\<fieldname\>"}

second object is {"value":"\<value\>"}

and it's not clear how I would parse this in Jira Automation. So far, I'm reviewing the Advanced Branching to assign {{webhookData.audit.changes}} to a variable, but I think there is some [json parsing](https://confluence.atlassian.com/automation/jira-smart-values-json-functions-993924865.html) or [list iterating](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-lists/) needed after that, and either converting to name:value pairs in a new json object, or dynamically using Create Variable (although it seems we cannot dynamically create names there).

Example

```
"changes" : [ 
  {"field_name":"Name", "value":"test4"}, 
  {"field_name":"Description", "value":"<div class=\"user-content\"><p>desc</p></div>"}, 
  {"field_name":"Workspace", "value":"OPTEM Templates"}
]
```

Any suggestions on how to convert the data so it can be accessed like this?

```
{{changes.name}}

{{changes.description}}

{{changes.workspace}}
```

Thanks ahead!


Answers:
author: Bill Sheboy
body: Hi [@Erin Quick-Laughlin](/t5/user/viewprofilepage/user-id/4299733)

You appear to want to change the structure of the response within the scope of the rule, and I am unclear how that is currently possible. The closest you may get is with match() and regEx to access the fields.

To save you time...created variables and list filtering may not help with this use case either: I experimented a while back to create name/value pairs in a created variable for access later in rules, but got stuck as many of the functions cannot take a variable as a parameter...and...variables are still text and do not understand any other typing information (e.g. JSON)

Kind regards,  
Bill

comments: author: Erin Quick-Laughlin
body: Thanks for the quick response [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) - really, I just want access to those values and then create a new Issue with them. For instance, how could one find the object where "field_name":"Name" exists, and pull the value from "value":"test4" (in this case, to set the Summary to "test4" when it calls Create Issue) ?

```
"changes" : [ 
  {"field_name":"Name", "value":"test4"}
]
```

I'm not very familiar with Regex, but I'm guessing I would need to match "Name" or "field_name":"Name" to get an index, then refer to changes.get(n).value ?


author: Bill Sheboy
body: Okay, this isn't pretty and I suspect it will get the "value" for a the field_name of "Name". Please reuse, as needed, and substitute each specific field's name for each usage.

```
{{webhookData.audit.changes.match(".*(\{.field_name.\:.Name.*\}).*").match(".*value.\:.(.*).\}")}}
```

How it works:

* starting at the "changes", look for matches on the field_name and return the entire node
* then the second match finds just the value part

Unfortunately, the regular expression parser for rules does not appear to support escaping double-quotation marks, so I substituted the generic . (any character).

For more ideas on what to try to match, please look at this reference, which is what Atlassian says their parse *is based upon* : <https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html>


author: Erin Quick-Laughlin
body: Quick note for those who got here: A colleague of mine helped me figure out an alternative way to get at the value I was looking for. It's a slightly different scheme we're dealing with, but should apply for any webresponse.body json that has an array, and some way to get to it's id / value pair.

In json, it looks like this:

```
{
  idea:
  {
    (...snipped...)
    custom_fields:
    [
      {
        id:(...snipped...),
        key:jira_issue_key,
        value: ABC-123
      }
    ]
  }

}
```

Using smart value iterational functions, we can pull that value and finally assign / use it!

```
{{#webResponse.body.idea.custom_fields}} {{#if(equals(key, "jira_issue_key"))}} {{value}} {{/}} {{/}}
```


author: Sankalp Chugh
body: Thank you soo much for posting this solution. I struggled a lot on this problem! Can't thank you enough!


author: Sankalp Chugh
body: I'm facing one issue with the above solution. I'm not able to put a {{smart value}} instead of "jira_issue_key"


author: Bill Sheboy
body: Hi [@Sankalp Chugh](/t5/user/viewprofilepage/user-id/5157688)

I believe that is correct behavior and you would need another solution approach, such as the regular expression. Once the rule is processing *inside of the iterator* for {{#webResponse...}}...{{/}} the scope is narrowed to what is visible at that point. In Erin's example, {{key}} is visible. But in yours the {{smart value}} is perhaps at a different scope.

Kind regards,  
Bill


author: Sankalp Chugh
body: Hi [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313)   

I guess you are right. The scope maybe different. I thought maybe we can access issue details inside that loop.

I will try to implement the regular expression approach.

Thanks,

Sankalp


author: krunoslav_majer
body: [@Erin Quick-Laughlin](/t5/user/viewprofilepage/user-id/4299733)

Quick question as I have a similar issue.

I got it all and makes loads of sense but I can't get it to iterate over my structure.   
Which action do you use to execute the iteration? Is it Advanced branching?