concatenate an item in to issue.labels.asJsonStringArray ?
Asked by Dane Kantner:

I'm trying to copy the array of labels from the original issue (when creating a new issue) _and_ add a new label at the same time... it seems to allow copy of old but not add at same time. I can get it to "add" two smart variables and have a variable for each of these, but the one is an array... and it gives formatting errors because it's not proper json when I add them next to each other. Is there a way to concatenate an item in to issue.labels.asJsonStringArray so it outputs one proper list?


Answers:
author: Mark Segall
body: Hi [@Dane Kantner](/t5/user/viewprofilepage/user-id/4353572) - If you're looking to add new labels, you could do so in a second action. For the first action, create the issue and copy the labels. Then create a branch on the newly created issue and edit the issue with the new fields you wish to add. Something like this:

![2022-02-18_15-04-09.png](https://community.atlassian.com/t5/image/serverpage/image-id/184293iA7B1B427669AC47B/image-size/large?v=v2&px=999 "2022-02-18_15-04-09.png")

comments: author: Dane Kantner
body: Thank you -- I didn't realize I could do a branch off of created issues specifically and was assuming I had to come up w/ the JQL to find it (and there is no valid JQL to find what I had just created, the label I'm trying to add is actually a unique identifier that I could search on but it doesn't exist yet)


author: Stefan Salzl
body: I was facing same behaviour yesterday and was reading through documentation:  
<https://support.atlassian.com/cloud-automation/docs/advanced-field-editing-using-json/#Adding-labels>

++**Documentation:**++The documentation says:

**Labels**A system field that is an array of text values.

```
`"labels" : ["examplelabelnumber1", "examplelabelnumber2"] `
```

and further:  
For fields that take arrays of text:

```
{
"fields": {
"labels": {{issue.parent.labels.asJsonStringArray}}
}
}
```

++**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")

[@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) , [@Mark Segall](/t5/user/viewprofilepage/user-id/4666637) :  
Did I get anything wrong regarding the field or stringArray? Or could that be a bug?

Best  
Stefan

comments: author: Dane Kantner
body: I am trying to take triggerIssue.labels.asJsonStringArray and add a value to it so that the original triggerIssue.labels are kept BUT ALSO adding another string value to that list.


author: Darryl Lee
body: [@Dane Kantner](/t5/user/viewprofilepage/user-id/4353572) Adding a label to the newly created issue should not affect the triggerIssue.   

As to [@Stefan Salzl](/t5/user/viewprofilepage/user-id/4616993)ssue, it looks like the "add" function can only take a string, not an array. I think there's a way to expand out the array but I need to test if it works with "add"... standby.


author: Dane Kantner
body: I'm not trying to modify the trigger issue, I'm just trying to copy it labels array *and* add one more item to it to it in the newly created issue. The labels.asJsonStringArray does return the labels from the original issue properly as expected, and I can use that to add to the created issue, but the documentation says you can't set or add the fields twice, so it really needs to be added in to that string array in order to properly solve this issue, hence the question on how to concatenate to that array. (The ability to add it after the fact as a branch edit is an *OK* work-around but it's not perfect b/c it does delay processing an extra second or two; a second edit is *much* more expensive than a local variable edit to the labels array and adding it in to the field from the origination of the new issue create; I wonder since the add to array functionality doesn't exist if I could use regex to transform the returned JsonStringArray to alter it to inject a new string?)


author: Darryl Lee
body: I really don't think that adding a label afterwards will slow it down that much, and it's a **much** more readable rule.

But since I'm a glutton for punishment...

Figured out how to make this work via [Advanced editing using JSON](https://support.atlassian.com/cloud-automation/docs/advanced-field-editing-using-json/) using a few replaces, along with a split, and then another conversion back to an array:
> {  
> "fields": {  
> "labels": {{issue.labels.asJsonStringArray.replace("\[","").replace("\]",",\\"new-label\\"").split(",").asJsonArray}}  
> }  
> }  
> }

Note that you can't set this using the normal field setting, because in that interface, the Labels field can only take strings that are separated out in the UI. I could not figure out a way to pass it a **list/array** directly, and giving it a comma-delimited **string** just created one really long label.


author: Dane Kantner
body: Thanks I will give that replace gig a whirl -- it's ugly to h ave to read, but I've seen worse. :P I will potentially need to test if the jsonarray is empty to begin with too for robustness sakes.

The issue of why a second edit is "expensive" compared to doing it all in one create or edit is I may be creating 20 issues sequentially rather fast, and each of those creates fires a trigger rule where it's then trying to group/classify the finding in to a "resource" issue type and creates a "resource" issue OR groups it in to that existing resource issue that may have just been created. In the subsequent triggers, a 1s delay means I have to essentially delay the actual finding creations themselves by the equivalent amount of time to guarantee it has been processed. I had to add a 1s delay in initial creations already after making the change as a branch edit. If I can take it from 1s extra processing to 10ms, that's a lot more throughput at EOD.


author: Darryl Lee
body: Ah, ok makes sense that you'd want to minimize extra branches with multiple issue creation.

Speaking of checking for an empty array, I didn't think about that until I tried to solve [@Stefan Salzl](/t5/user/viewprofilepage/user-id/4616993)'s problem using a similar approach.

Please see [my updated code below](https://community.atlassian.com/t5/Automation-questions/Re-Re-concatenate-an-item-in-to-issue-labels-asJsonStri/qaq-p/1951503/comment-id/3374#M3374), that in fact checks to make sure the triggering issue's Labels array isn't empty.


author: Philippe Lassalle
body: [@Stefan Salzl](/t5/user/viewprofilepage/user-id/4616993) I have the exact same issue as you mentioned. I couldn't find a way to make **update** attribute with **add** operation works with an array as it works for **fields** attribute. Did you manage to find a way to make this work?

[@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) thanks for the workaround you provided, the fact that it relies on the **fields** attribute (shortcut to **update** attribute with **Set** operation) doesn't help when we want just to use **update** with **add** operation. Any reason why update/add doesn't work as expected?


author: Darryl Lee
body: Hi [@Philippe Lassalle](/t5/user/viewprofilepage/user-id/351345) - can you post what you've tried, and what errors you may have gotten?

Are you getting the same error as [@Stefan Salzl](/t5/user/viewprofilepage/user-id/4616993)?
> Operation value must be a string (labels)

The problem was Stefan was trying to basically do:

```
"add" : "Label1, Label2"
```

From [the documentation](https://support.atlassian.com/cloud-automation/docs/advanced-field-editing-using-json/#Adding-labels), when you use **add** for **labels** (notably if you have multiple labels) they have to be in this format:

```
{
   "update": {
      "labels": [
         {
             "add": "Label1" 
         }, 
         { 
             "add": "Label2" 
         } 
      
      ] 
   }
}
```

So use update **labels** is not a simple array which would work for **fields** . It is an array of **add** objects, and you have to format it accordingly.


author: Philippe Lassalle
body: [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) thanks that clarifies, I'll head into that direction, meaning formatting the issues.labels array accordingly.


author: Darryl Lee
body: The other approach that is fiddlier, but potentially more powerful, is to use Advanced Edit as part of Create New Issue:

Luckily there's an [example in the documentation](https://support.atlassian.com/cloud-automation/docs/advanced-field-editing-using-json/)!

I think this would work:

    {    "update":         "labels": [{                "add": "extra-label"        }]    }}

comments: author: Darryl Lee
body: Oops, my idea above isn't quite right, because I ended up with this error when trying to both **Copy Labels from the Current Issue** \*and\* add the extra one in the **More options -\> Additional fields** JSON section:  
> Error creating issue  
> Field 'labels' cannot appear in both 'fields' and 'update'

So then, as mentioned in the other thread, here's what ultimately **did** work, in case you wanted to also mark this answer as correct, since I think it's a little closer to what you were looking for.

Again, this would go in the **More options -\> Additional fields**section when you Create a new issue:
> {  
> "fields": {  
> "labels": {{issue.labels.asJsonStringArray.replace("\[","").replace("\]",",\\"new-label\\"").split(",").asJsonArray}}  
> }  
> }  
> }


author: Darryl Lee
body: Ooof, I realize this "fix" has the same problem as what I thought was going to fix [@Stefan Salzl](/t5/user/viewprofilepage/user-id/4616993)'s [issue](https://community.atlassian.com/t5/Automation-questions/Add-not-replace-labels-from-blocking-issue-to-blocked-issue/qaq-p/1950582#M3373):

I didn't account for the possibility that the original issue may not have \*any\* labels. Augh. So in that case I end up with an extra leading comma, which is not valid JSON:

```
"labels": [,"new-label"]
```

Ok, the fix is fixed. Ugh, sure would be nice if Automation had an ELSE function.

```
{
? ? ?"fields": {
? ? ? ? ?"labels": [
? ? ? ? ? ? ? ?{{#if(not(issue.labels.isEmpty))}}
? ? ? ? ? ? ? ? ? ? {{#labels}}"{{.}}"{{^last}},{{/}}{{/}},"new-label"
? ? ? ? ? ? ? ?{{/}}
? ? ? ? ? ? ? ?{{#if(issue.labels.isEmpty)}}
? ? ? ? ? ? ? ? ? ? "new-label"
? ? ? ? ? ? ? ?{{/}}
? ? ? ? ?]
? ? ?}
}
```

This is \*much\* cleaner than all of that JsonArray hackery though. Whew.


author: Marc Isikoff
body: Automation does have an "Else" function within branching for If statements it can create at the bottom of the automation paragraph an else that you can code. But if you mean "else" in the context of Additional Fields, then no.


author: Thomas Marshall - Network
body: [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) Thank you!


Tags: cloud