Issues with the replaceAll function. How can I remove special characters [, ], " from string?

John Izquierdo March 7, 2024

Hi,

I'm I'm trying to remove specific characters '[', ']', and '"' (quotation marks) from a 'value' in the description. Below is the value.

Additional Hardware Selected: ["laptop dock", "laptop", "keyboard", "mouse"]

I'm first using create variable function in an automation to create a variable with the name {{msformAddHardware}} and the value {{issue.description.match("Additional Hardware Selected: *(.*)")}}.

Then, I'm using the Audit Log function with the smart-value {{msformAddHardware.replaceAll("[\\[\\]\"]", "")}} but the Audit log shows the log function is empty.

My purpose for this is, in the automation, to use the created variable to set the value for a multi-select custom field.

Please help. Thanks! 

2 answers

2 accepted

2 votes
Answer accepted
William Calesco
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!
March 7, 2024

Hi John,

I know that character Classes work for replaceAll().

If you are getting this "[\\[\\]\"]" (isn`t there an extra " ?) from different parts of the text,

try chainning the characters, for example:

{{issue.summary.replaceAll("[\\]", "").replaceAll("\", "")}}

 and so on. Start with the first character, test, and then progress adding the others.

John Izquierdo March 12, 2024

Hi, William

The Log Action was returning the error below.

Failed to get value for msformAddHardware.replaceall("[\\]", ""): {{msformAddHardware.replaceall("[\\]", "")}}

I ended up using: value {{msformAddHardware.replaceAll("[\\[\\]\"]", "")}} to create a new variable "addHard". Then, created a new variable "selectHardware" with the value {{addHard.trim()}}.

I might have over done it with all the created variables but nonetheless.

Then, with in the "Edit" function I used the following JSON.

{
"fields": {
"customfield_12487": [
{{#selectHardware.split(",")}}
{"value": "{{.}}"}{{^last}},{{/last}}
{{/}}
]
}
}

 

1 vote
Answer accepted
Kalyan Sattaluri
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 7, 2024

Hello @John Izquierdo 

 

Yes, as @William Calesco  has mentioned, try chaining the "replace" function to replace one at a time.

Once you have a clean list separated by commas in your variable (which is still text), please check the method demonstrated by @Bill Sheboy in below post on how to format your text to JSON so you can set/update your field accordingly.

https://community.atlassian.com/t5/Automation-questions/Converting-list-to-JSON-object/qaq-p/2551363

John Izquierdo March 12, 2024

Thanks, Kalyan!

I was still confused reading that post (still kind of new to Jira and getting familiar with smart-values but don't know a lick of JSON), thankfully we're working with some devs from OnRamp who provided the same solution and was able to explain it/how it works.

Like Kalyan Sattaluri likes this
Kalyan Sattaluri
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 12, 2024

Yes, It takes a couple of tries to absorb how to iterate a list and format the JSON to get it so you can update corresponding fields.

And that method of @Bill Sheboy  is one of my absolute favorites I have ever seen. Its just such a simple yet, out of the box solution that my mind was blown away and i am still impressed, lol.

For reference, if you look at below Jira KB article on "how else" it could have been achieved, its just confusing and feels round about. That's what i admire about that solution.

https://confluence.atlassian.com/jirakb/converting-a-csv-string-to-a-json-object-array-when-json-smart-value-functions-do-not-work-in-an-automation-rule-1189506509.html

BTW, can I please ask you to accept @William Calesco answer as well, his solution was the correct one so folks researching in the future can benefit by quickly getting to the answer. Thanks!

Like John Izquierdo likes this
John Izquierdo March 12, 2024

Ah! I might have been thinking of a different community post with a similar question to mine. I didn't try, but still don't fully understand the article you linked, I'll have to stare at it for a while and test it out and fail a couple of time. lol

I explained what method I ended up using in a reply to @William Calesco... I might have used the roundabout way of doing it tbh.

Once I figure out and successfully use the method provided in that article I will be sure to update my automation so it doesn't feel like a mess.

Thanks again!

Like Kalyan Sattaluri likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events