Add (not replace) labels from blocking issue to blocked issue
Asked by Darryl Lee:

[@Stefan Salzl](/t5/user/viewprofilepage/user-id/4616993) had an interesting question as part of [this discussion](https://community.atlassian.com/t5/Automation-questions/concatenate-an-item-in-to-issue-labels-asJsonStringArray/qaq-p/1949683#M3329) that I think I figured out the answer to.

**Stefan's post:**

++**Description of the behaviour:**++I want to add more than 1 labels from one issue to another one (eg. from parent to child) whereas the let?s say the child already got 2 labels and the parent has 2 different labels.

I tried to update a linked issue?s label field as follows:

```
{
"update": {
"labels": [
{
"add": {{destinationIssue.labels.asJsonStringArray}}
}
]
}
}?
```

but audit log output says looks like:

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/184325i500CD5F82F696135/image-size/large?v=v2&px=999 "image.png")


Answers:
author: Curt Holley
body: Am I missing something? Can't this be done as simply as:

![Screenshot 2022-02-22 085657.png](https://community.atlassian.com/t5/image/serverpage/image-id/184614iA730EBFB926D5A78/image-size/large?v=v2&px=999 "Screenshot 2022-02-22 085657.png")

you just have to make sure you select the "Add/remove" operation.

![ar.png](https://community.atlassian.com/t5/image/serverpage/image-id/184615i3BA501D4294FC6EA/image-size/large?v=v2&px=999 "ar.png")

comments: author: Stefan Salzl
body: Hooooly moly..... :o :o how could I oversee this option??? OOOOMG......

Thank you very much [@Curt Holley](/t5/user/viewprofilepage/user-id/3527535) (even if I invested the whole evening in trying to find a solution for this haha)

Well....learned a lot. therefore: it?s worth it.

Thanks guys for that great solution and lesson :)


author: Curt Holley
body: Yes [@Stefan Salzl](/t5/user/viewprofilepage/user-id/4616993) I recall having a similar reaction ? upon finding the add/remove operation.

Enjoy!!


author: Stefan Salzl
body: [@Curt Holley](/t5/user/viewprofilepage/user-id/3527535) hahaha......so funny..... can?t stop lauging =D


author: Darryl Lee
body: So as I mentioned in the other post, the error you got is because the "add" function can only take a string, not an array.

But after solving Dane's issue with a bunch of ugly Smart Value JSON hackery, I realized your problem wouldn't be much harder. It requires a temporary variable, because once we branch to the Destination issue, we apparently can't access the original issue's field values.

Basically we set a Smart value variable **originalLabels** (from the blocking issue):

```
{{issue.labels.asJsonStringArray.replace("[","").replace("]",",")}}
```

And then we Branch to the Destination issue (what's being blocked) and edit using **More options -\> Additional fields:**
> {  
> "fields": {  
> "labels": {{originalLabels.concat(destinationIssue.labels.asJsonStringArray.replace("\[","").replace("\]","")).split(",").asJsonArray}}  
> }  
> }  
> }

So it ends up looking like this:

![Screen Shot 2022-02-21 at 1.12.03 AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/184466iDF56F3A0B89AF74D/image-size/large?v=v2&px=999 "Screen Shot 2022-02-21 at 1.12.03 AM.png")

Let me know if this works, and if so, can you vote for this answer?  

Thanks!

comments: author: Stefan Salzl
body: uff.....a lot of things for me to learn from this example :) Haven?t been using smart value functions but will definitly take some deeper insights to understand what is possible with replace, split, etc.... :o :o

unfortunately the solution doesn?t work. See my audit log:

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/184531i5D4897DEBB8A4DAF/image-dimensions/338x194?v=v2 "image.png")


author: Darryl Lee
body: AH, I think I forgot to take into account this case:

\* If the blocking issue (**originalLabels**) does not have any labels, then I should not include a comma.

Basically I ended up it trying to update with this invalid JSON for labels:  
> "labels": \[**,**"childlabel1","childlabel2"\]

(Note the extra leading comma.)

Ugh.... I think this can be fixed with some [conditional array trickery](https://community.atlassian.com/t5/Marketplace-Apps-Integrations/If-else-in-Automation-for-Jira-advanced-field-editing-JSON/qaq-p/1326221) or by adding a condition to skip the edit [in case the blocking issue has no labels](https://community.atlassian.com/t5/Jira-Software-questions/Automation-Copy-multiselect-values-from-parent-to-child-JSON/qaq-p/1934405), but [@Curt Holley](/t5/user/viewprofilepage/user-id/3527535) has already provided a much. better. answer.

I'm gonna take a nap. :-}


author: Darryl Lee
body: I lied. :-}

I couldn't help myself. Here's the corrected JSON:

```
{
? ? ?"fields": {
? ? ?? ? ?"labels": [
? ? ?? ? ?? ? ?{{#if(not(triggerIssue.labels.isEmpty))}}
? ? ?? ? ?? ? ?? ? ?{{#triggerIssue.labels}}"{{.}}"{{^last}},{{/}}{{/}},
? ? ?? ? ?? ? ?? ? ?{{#destinationIssue.labels}}"{{.}}"{{^last}},{{/}}{{/}}
? ? ?? ? ?? ? ?{{/}}
? ? ?? ? ?? ? ?{{#if(triggerIssue.labels.isEmpty)}}
? ? ?? ? ?? ? ?? ? ?{{#destinationIssue.labels}}"{{.}}"{{^last}},{{/}}{{/}}
? ? ?? ? ?? ? ?{{/}}
? ? ?? ? ?]
? ? ?}
}
```

And the **originalLabels** variable is no longer required.

Obviously this is overly-complicating things, but I'm putting this here in case anybody else has to do something a little trickier with conditionals in JSON.

It's not quite as much of a headache as I initially feared.

One thing I thought of - if a blocking linked issue is **unlinked**, I don't know if you want to remove its labels from the blocked issue, but if so, you'd need another rule.

Additionally, you'd want to make sure that those labels in any other blocking tickets.

Yay, more conditions. :-}


Tags: jira-cloud