Sending valid json via API

I’m trying to have my app send an email containing field data, and using Mailchimp/Mandrill API for this. However, I can’t get the body of the message fully escaped in order to avoid errors. I’ve used a basic .replace in the json body, e.g.

{{requestsTable.selectedRow.detail.replace("'","'")}}

which works with the email being sent, but only replaces the first occurrence as would be expected of that function. However, when including the RegEx function within that replace, although the ‘Evaluated Value’ reads correctly with encoded characters, an error is produced:

Plugin failed to parse JSON "{"key":"XXXXXXXXXXXXXXXXXXXXXXXXX",
 "message":{
	 "html":'<html><head><title>Support Request: "academic"</title></head><body><p>My Name:"Pietro" "Vincenzo"</p><p> <strong>My Support Request:</strong> '{{requestsTable.selectedRow.detail.replace(RegExp(/'/g),"&apos;")}}'</p><p><strong>My Email:</strong>'{{requestsTable.selectedRow.email}}'<p></body></html>',
	 "subject":"HelpDesk App Test",
	 "from_email":"enquiries@oca.ac.uk",
	 "from_name":"OCA Testing Centre",
	 "to":[{"email":{{assignTo.selectedOptionValue}}, "name": "{{assignTo.selectedOptionLabel}}"}]
	}
 }
" with error: Malformed JSON: Unexpected token { at position 198.

The error suggests the first { is being interpretted as a new nested schema, but why when the json works without the RegEx function?

The same occurs if I try to use the same regex when first setting the data in the table, leading me to suspect it’s my lack of js and regex knowledge at play? E.g. similar error given for this, despite the variables rendering correctly in the ‘Evaluated Value’ box:


INSERT INTO requests
  (id, firstName, lastName, email, type, priority, detail)
VALUES
  (
    {{id.text}},
    {{firstName.text}},
    {{lastName.text}},
		{{email.text}},
		{{type.selectedOptionValue}},
		{{priority.selectedOptionValue}},
		{{desc.text.replace(RegExp(/'/g),'&apos;')}}
  );

Any ideas?

Many thanks
Paul

Edit: I just realised the RegEx function isn’t required, but it still produces the same error when entering {{desc.text.replace(/'/g, '&apos;')}}

@mrvinceo can you try using replaceAll?
{{requestsTable.selectedRow.detail.replaceAll("’","’")}}