Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Escaping '$' character in Description field text inside JSON REST API POST

My replace function below seems to work for other special characters but cannot get it to work for $ symbol - any advice would be most appreciated as I've tried all variations I can think of and have read so far...

def jsonEscape = {

it.replace("\\","\\\\")

.replaceAll("\r","\\\\r")

.replaceAll("\n","\\\\n")

.replaceAll("\t","\\\\t")

.replaceAll("\"","\\\\\"")

//.replace("\$","\\\\\$") 

}

 

1 answer

1 accepted

1 vote
Answer accepted

Finally figured this out, so am posting answer in case it helps someone else...

the solution is:

.replace("\$","\\\$")

note 3 backslashes in replace, presumably 2 backslashes to escape then \$ to escape in JSON.

Anyways, it works nicely...

Good day,

I am new to API's and do not know where to place this portion of the code.

Will you please assist

Hi Tommie,

its difficult for me to answer properly without knowing what you're trying to achieve. In my case the code was in a groovy file referenced inside a Jira Workflow process step. The first part of the code - see below, is called a Closure in groovy and can be called multiple times in the remainder of the code. I usually place Closure code at the beginning, just after the code to import library files.

//** Removes CR/LF & Tab characters that break the JSON Parser and replaces them with escaped characters **//
def jsonEscape = {
    it.replace("\\","\\\\") // Java Regex needs an escape before backslash to pick up, hence look for \\ but to escape in JSON needs to escape that so \\\\
    .replaceAll("\r","\\\\r") // carriage return
    .replaceAll("\n","\\\\n") // new line
    .replaceAll("\t","\\\\t") // tab
    .replaceAll("\"","\\\\\"") // double quote
    .replace("\$","\\\$") // adds backslash prefix to $ symbol
}

Below are two examples which call the above Closure:

def cleanSummary = jsonEscape(issue.getSummary())
def cleanDescription = jsonEscape(issue.getDescription())

I hope this helps...

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Why everyone using Jira must be GDPR-compliant

Did you know that penalties up to 4 % of the yearly company turnover are possible in case of GDPR violations? GDPR regulations are currently mainly relevant for companies in the EU, but countries lik...

103 views 1 3
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you