on 04-19-2023 2:19 PM
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?
Please try to use regex to overcome the double and single quotes problem.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
const regex = /('(?=(,\s*')))|('(?=:))|((?<=([:,]\s*))')|((?<={)')|('(?=}))/g;
str = str.replace(regex, '"');
Single and double quotes can be replaced in above code as per the requirement.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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"}
User | Count |
---|---|
83 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.