cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy Script error parsing json payload in CPI

ayushtiwari
Participant
0 Kudos
1,739

Hi All,

I am passing a Json payload to CPI. Value is changing for integer so i want to replace that in CPI using groovy script.

Payload coming into CPI is :

{ "Name" : "Unhappy Customer Survey Feedback Response", "BuyerPartyID" : "\"1049913\"", "DataOriginTypeCode" : "\"4\"", "ServicePriorityCode" : "\"1\""}

want to change it in CPI like:

{ "Name" : "Unhappy Customer Survey Feedback Response", "BuyerPartyID" : "1049913", "DataOriginTypeCode" : "4", "ServicePriorityCode" : "1"}

below is the groovy script which I am using.

script.txt

Please help me to resolve this issue.

sunilchandra007
Active Contributor
0 Kudos

Instead of having additional step of groovy script, you could try using the first content modifier in you flow with below simple expression.

${body.replaceAll( '\\\"', '')}

Regards,

Sunil Chandra

ayushtiwari
Participant
0 Kudos

I tried to put ${body.replaceAll('\\\"','')} in body but getting below error

Accepted Solutions (1)

Accepted Solutions (1)

ahmed_t
Participant

This should be your escape sequence:

String input = "\"1049913\"";
input1 = input.replaceAll(/\\"/,/\\\\"/);
return input1

Regards,

Toushif.

ayushtiwari
Participant
0 Kudos

Hi Toushif,

I am testing the string in groovy console then its working. If i am passing payload { "Name":"Unhappy Customer Survey Feedback Response", "BuyerPartyID":"\"1049913\"", "DataOriginTypeCode":"\"4\"", "ServicePriorityCode":"\"1\"" } in CPI then I am getting error. I am new to this and dont know how to write script, so my script is not working in CPI.

ahmed_t
Participant
0 Kudos

This happens sometimes in the Groovy web console. It requires a extra backslash(\) to be escaped however, please find the required escape sequence tested in CPI.

Use the below code in Groovy Script:

Result:

Regards,

Toushif.

ayushtiwari
Participant

Thank You Toushif. it helped and working fine now 🙂

Answers (1)

Answers (1)

0 Kudos

Hi ayushtiwari,

I think you should use replace on variable body at one go.

body = body.replaceAll('\"','')

And then parse it later.

Hope this helps !

thanks and regards,

Praveen T

ayushtiwari
Participant
0 Kudos

Hi Praveen,

Do i need to use Jsonbuilder to return this message as json.

Thanks,

Pintu