Skip to Content
1
Apr 19 at 01:19 PM

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

121 Views Last edit Apr 19 at 02:12 PM 2 rev

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?