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

How to parse through an json array where field_name is a separate object from value

Erin Quick-Laughlin
Contributor
November 14, 2022

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 or list iterating 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!

 

 

1 answer

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.
November 14, 2022

Hi @Erin Quick-Laughlin 

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

Erin Quick-Laughlin
Contributor
November 15, 2022

Thanks for the quick response @Bill Sheboy - 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 ? 

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.
November 15, 2022

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 uponhttps://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Like # people like this
Erin Quick-Laughlin
Contributor
March 13, 2023

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}} {{/}} {{/}}
Like # people like this
Sankalp Chugh
Contributor
April 13, 2023

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

Like Erin Quick-Laughlin likes this
Sankalp Chugh
Contributor
April 13, 2023

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

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.
April 13, 2023

Hi @Sankalp Chugh 

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

Sankalp Chugh
Contributor
April 13, 2023

Hi @Bill Sheboy 

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

Like Bill Sheboy likes this
krunoslav.majer November 24, 2023

@Erin Quick-Laughlin 

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?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events