cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert double quotes inside the JSON formatted payload to single quotes

Hi,

My target system only understands the JSON format. We are getting data from EC, which is an xml format. We are saving all the required values in properties and for every target required field, we have specific conditions to meet and then need to call the property value based on the condition. So groovy was used to prepare the Json format payload.

Like

def value = "'values': { ";

firstName = map.get("firstName");

lastName = map.get("lastName");

if (firstName != null){

value = value + "'First Name':'"+ firstName + "',";

};

if (lastName != null){

value = value + "'Last Name':'"+ lastName + "',";

};

value = value + "}";

def payload = value.replaceAll("\'","\"")

message.setProperty("values", payload);

return message;

After preparing the payload, all the single quotes(') were replaced with double quotes("). But now the problem is, we are getting single quotes(') in first name from source, now as we are replacing completed payload single quotes(') were replaced with double quotes("). It's causing issue. So, could you please advise me, how to fix this?

Ryan-Crosby
Active Contributor
0 Kudos

Single quotes are invalid syntax for JSON so the sender should fix the formatting.

0 Kudos

Hi ryan.crosby2,

Thanks for the quick reply.

We are fetching xml payload from EC. As the target system need JSON format, we are forming the json payload using groovy.

Ryan-Crosby
Active Contributor
0 Kudos

mohanty.atulkumar yes, I understand that... it still doesn't make single quotes in JSON valid... it should be fixed at the source (where you are picking it up).

Accepted Solutions (0)

Answers (2)

Answers (2)

Harish_Vatsa
Active Contributor

Please try to use regex to overcome the double and single quotes problem.

0 Kudos

Could you please suggest the code. I tried to add the regex, but that doesn't worked out. Seems I am trying wrongly, that's the reason asking for the code.

Harish_Vatsa
Active Contributor
const regex = /('(?=(,\s*')))|('(?=:))|((?<=([:,]\s*))')|((?<={)')|('(?=}))/g; 

str = str.replace(regex, '"');

Single and double quotes can be replaced in above code as per the requirement.

0 Kudos

Hi harishvatsa,

Thanks for the quick suggestion. But const in not in use in groovy. So the code is giving error.

Sriprasadsbhat
Active Contributor
0 Kudos

Hello atul,

Do you have some payload which can be used for try out .Also use CODE formatter in community text editor while placing scripts and input xml for easy copy paste.

Regards,

Sriprasad s Bhat

0 Kudos

Hi shriprasad.bhat,

Thanks for the reply.

When payload was formed by calling the properties.

The initial formed payload will be like the below

'values': { 'First Name':'Hari Sophie','Last Name':'Pv'olt'}

After that, replacing single quotes with double quotes for complete payload

def payload = value.replaceAll("\'","\"")

This is how the payload looks like after the replaceall

"values": { "First Name":"Hari Sophie","Last Name":"Pv"olt"}