If else in Automation for Jira advanced field editing JSON

Erik Buchholz
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.
March 17, 2020

I'd like to have a if else statement inside a JSON for the advanced field editing

https://support.atlassian.com/jira-software-cloud/docs/advanced-field-editing-json/

I have some cases where I have a if else condition to check if a field is empty because the empty field makes the JSON invalid.

If the field has a value the following example JSON is valid.

{
"fields": {
"My Custom Array":
{{My Custom Array.asJsonObjectArray("value")}}
}
}

If the field is empty, the smart value does not return anything, and the JSON gets invalid.

I tried the if from the math expressions.

https://support.atlassian.com/jira-software-cloud/docs/smart-values-math-expressions/

Here are only numeric values allowed, so I'm not able to manipulate the JSON before the empty field.

Is there any function to avoid the '"My Custom Array":' or to get a valid empty JSON object array?

I'm looking for a solution for arrays and single value fields.

2 answers

1 accepted

2 votes
Answer accepted
Erik Buchholz
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.
March 17, 2020

For array/list fields I found the following solution.

    {{#My Custom Array}}
{{#first}}"My Custom Array": [ {{/}}
{{value.asJsonObject("value")}}
{{^last}} , {{/}}
{{#last}} ] {{/}}
{{/}}

Before the first value of the array and therefore only if there is a first value we write the '"My Custom Array": ['. The downside of this solution is, that you cannot use the asJsonObjectArray(key) function.

Update 2020-05-12

@Petar Minchev 

If found another solution for arrays and one for single values:

{
"fields": {
"label_like_customfield": [
{{#label_like_customfield}}"{{.}}"{{^last}},{{/}}{{/}}
],
"array_customfield_1": [
{{#array_customfield}}{{asJsonObject("value")}}{{^last}},{{/}}{{/}}
],
"array_customfield_2": [
{{#array_customfield}}{{value.asJsonObject("value")}}{{^last}},{{/}}{{/}}
],
"single_vslue_customfield": {
"value": "{{single_value_customfield}}"
}
}
}

With this code you get an empty array or an empty value resp. if the fields are empty.

The code may need some modification depending on the custom field type. Please note that some array custom fields needs you to go into the value to call the asJsonObject() function as in array_customfield_2.

Update 2022-06-23

I found some documentation about chaining JSON functions, but these have the same problem that they produce nothing instead of an empty array.

Petar Minchev May 12, 2020

Hi @Erik Buchholz and thanks for update i test it but json is still not valid. Note: customfield_12 is mandatory and there is always value, but customfield_123 is not mandatory and when its empty (array is empty) json fails. This is it what i'm testing:

{
"fields": {
"customfield_12": {{issue.customfield_12.asJsonObjectArray("value")}},
"customfield_123":{#issue.customfield_123.asJsonObjectArray("value")}}"{{.}}"{{^last}},{{/}}{{/}}
}
}
Erik Buchholz
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.
May 13, 2020

Hi @Petar Minchev ,

so customfield_12 is never empty, right? Try the following code

{{#debug}}
{
"fields": {
"customfield_12": {{issue.customfield_12.asJsonObjectArray("value")}},
"customfield_123": [
{{#customfield_123}}
{{value.asJsonObject("value")}}{{^last}},{{/}}
{{/}}
]
}
}
{{/}}

With the debug tag you'll see the result in the audit log.

Then you need to open the array for customfield_123 manually, add an Json object for every entry in customfield_123, and add a comma for every but the last entry. Then you need to close the array manually again.

If it still fails, give us a sample of the result JSON.

Like # people like this
Petar Minchev May 13, 2020

Hi @Erik Buchholz i've just tested the code and its working. When the field is empty creates empty array and when filed its not empty - value are copied correctly. There is only little correction in the code fields need to be open and closed with { } , not with [ ]. Thanks a lot and Cheers.

"fields": { ....... everything else it the same ...

}

Like Erik Buchholz likes this
Erik Buchholz
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.
May 13, 2020

Hi @Petar Minchev ,

thanks for your reply. I corrected the code and added the example to the answer. Would you mind to vote for my answer?

Best regards,
Erik

Petar Minchev May 13, 2020

@Erik Buchholz its already masked as "Answer accepted". Have a great day :)

Like Erik Buchholz likes this
Jesse Hamburger
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 22, 2022

This was astonishingly helpful and I managed to stumble my way into finding your answer just as I was at the end of my rope, so thank you. Can you explain what the various smart value objects are actually doing? particularly the "{{^last}}," bit. I've not been able to find documentation on this anywhere so far.

Erik Buchholz
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.
June 23, 2022

Hi @Jesse Hamburger ,

{{^last}}...{{/}}

is part of the list syntax. You can find the documentation at https://support.atlassian.com/cloud-automation/docs/jira-smart-values-lists/ within Combined function examples.

Regards

Like Jesse Hamburger likes this
0 votes
Petar Minchev May 4, 2020

Hi Erik did you find a way to solve this problem because i have the same, i'm using automation and i'm taking values from current issue and create new one but i have 2 custom fields for which i need to use asJsonObject("value") smart value and if CF is empty JSON become valid.

Maximilian Floß January 25, 2024

@Petar Minchev Do you have solution?

Petar Minchev
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 31, 2024

Hi @Maximilian Floß ,
the solution is upper in the thread from Erik Buchholz May 13, 2020

{{#debug}}
{
"fields": {
"customfield_12": {{issue.customfield_12.asJsonObjectArray("value")}},
"customfield_123": [
{{#customfield_123}}
{{value.asJsonObject("value")}}{{^last}},{{/}}
{{/}}
]
}
}
{{/}}

its working fine for me

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events