Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,461,700
Community Members
 
Community Events
176
Community Groups

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...

Like Gordon McDonald likes this

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events