Parsing " for JSON

:bullseye: What is your goal?

Create valid JSON to send into SaaS application

:thinking: What is the problem & what have you tried?

Hi Guys,

I’m going crazy here.

I have an email signature in the db that is in html format. I am trying to convert it to valid JSON and send it the the front end but I cannot for the life of me get it to replace " s with ".

I have tried every GPT under the sun and no matter what changes I make make seems not to be able to replace it.

Here is the expression I have in a set variable module at the moment:

{{replace(replace(replace(9.signature; “”“”; “"”"); “\r\n”; “\n”); “\n”; “\n”)}}

And this is the result:



Regards,

External Media Tim
Enterprise Account Executive

+64 401
tim@tim.com
tim.com

External Media

Any help would be appreciated :slight_smile:

:clipboard: Error messages or input/output bundles

Not valid JSON

Welcome to the Make community!

Escaping strings before mapping into another JSON string

Most of the time, you cannot directly map variables containing text content into another JSON string without escaping them, as the variable might contain special characters that are reserved. Special characters in JSON needs to be escaped (encoded) as a “literal” character, otherwise they make the whole JSON invalid when you map the variable value when creating another JSON string.

You can escape string variables by passing the text into the “Transform to JSON” module. You can also build a valid JSON string by using a Data Structure with the “Create JSON” module. Then, you can map the output of the Transform module into another JSON string, or simply use the whole output of the Create module.

:warning: Note:

When using the “Transform to JSON” module on a string type variable, the output should already contain the double quotes " " around your text, so when you map this output into your JSON, you should not need to wrap another pair of double quotes around it.

Alternatively, you can use the built-in function replace to replace those special characters with their escaped versions. To do this, simply paste this into the field, and replace the 1.text variable:

{{ replace(replace(1.text; "/([""\\])/g"; "\$1"); "/\r?\n\r?/g"; "\n") }}

For more information on JSON and escaping JSON strings, see:

Hope this helps! If you are still having trouble, please provide more details.

@samliew