Is there a general module to escape characters of a JSON for HTTP Request?

Hi,

I want to escape all possible characters of a string to send it in a HTTP Request in a JSON

I need something like this;

 .replaceAll("\"","'")
      .replaceAll(/[\\]/g, '\\\\')
      .replaceAll(/[\/]/g, '\\/')
      .replaceAll(/[\b]/g, '\\b')
      .replaceAll(/[\f]/g, '\\f')
       .replaceAll(/[\n]/g, '\\n')
      .replaceAll(/[\r]/g, '\\r')
      .replaceAll(/[\t]/g, '\\t')

Is there any in-built module to do this? Or should I replace one by one?

You could use the Set a Variable module and for the variable value, you can have a series of nested replace() functions that apply each of your replace tasks recursively. You can see an example here: Multiple replace functions in one value - #2 by JugaadiTech

That would be less messy compared to just having 8 text replace modules.

2 Likes

If there are many and you are sending a JSON body, the best thing will be to use Create a JSON module instead, which will handle all the escaping required, without you needing to use replace function for each one of them.

I am not exactly sure on the use-case of what you have mentioned, it it is the limitation on the API vendor not supporting such characters then using replace as suggested in the previous answer is the only option.

But Using Create a JSON module if applicable for you will be the best thing to do.

2 Likes

Hey @Runcorn ,

Do you know any solid way to create JSON?

It keeps giving me this error

image

I’m generating a structure with this and when I try to use it, it gives this error

{
    "ColumnstoChange": [
        {
            "columnId": "status2",
            "columnValue": "{{49.`8`}}"
        },
        {
            "columnId": "datum",
            "title": "Datum Sonderanalyse beantragt",
            "columnValue": {
                "date": "{{49.`80`}}",
                "includeTime": false
            }
        },
        {
            "columnId": "checkbox",
            "columnValue": {
                "checked": "{{if(74.`Sonderanalyse erfolgt?` = 0; null; true)}}"
            }
        }
    ]
}

The one that you are using is invalid, remove the extra curly braces after the ColumnstoChange, It should be like this,

{
	"ColumnstoChange": [{
			"columnId": "status2",
			"columnValue": "{{49.`8`}}"
		},
		{
			"columnId": "check",
			"columnValue": {
				"checked": "{{if(49.`9` = 0; null; true)}}"
			}
		},
		{
			"columnId": "checkbox",
			"columnValue": {
				"checked": "{{if(74.`yes` = 0; null; true)}}"
			}
		}
	]
}
2 Likes

@Runcorn ,

I’m sorry but this was not the problem, I already fix it before generating but paste the wrong JSON in here.

Do you know any example to create json out of json? Because I already mapped like 96 columns in JSON and I dont want to was my time with mapping in Array Aggregator again.

If you want to use the Parse JSON module, the best way to do this will be to create a data structure as the JSON that you have shared have a fixed structure from the looks of it, so you can either generate a new one or create a new structure. For the escaping to work, You will need to utilize the Data Structure.

Can you share how your scenario looks like and how you are getting the initial JSON?

2 Likes

Hey @Runcorn ,

This is the flow I’m trying to find a solution to;

I’m trying this with your recommendations;

I cannot just fit it to the data structure. When I generate a data structure out of the one given above, it doesnt accept.

This is the generated structure;


This is Create Json with Json;


Hey @Runcorn ,

any thoughts?

Have a look at this screenshot. I think that not all columns to change instructions match the data structure:

1 Like

Hey @Loopz ,

Thanks for your answer!

Do you have any idea how to use both of them in a structure?

Because I generate the structure with a sample of text and collection like you pointed out and it still gives me invalid error

I am able to generate a JSON array containing both the text-like and collection-like replacements.

As object:

And as JSON:
image

Drawback: it costs more operations than I would want. There should be a much simpler way…

here’s the scenario, feel free to try it out:
escaped-merged-json-objects.json (19.6 KB)

here’s how it looks like:

In case you want to apply it to your scenario, make sure to add the ‘ColumnstoChange’ field in the configuration of the monday.com module

2 Likes

5 operations is the closest I get:

escaped-merged-json-objects-v2.json (46.7 KB)

Curious to find out if someone can help us out and share a better solution!

This last attempt is a bit hacked together, assuming that none of the column names in Monday or replacement values will contain a pipe char “|”. If so, you can replace the char with something else you never expect. So this could work, until it breaks if the value does appear in the replacement values :stuck_out_tongue:

2 Likes

Hey @Loopz ,

Thank you for your quality answers! Its very challenging :smiley:

I’m trying your scenarios but I dont have the “data structure” to use with you scenarios. Can you share the data structure too?

Yes, here they are:

for text-like replacements

for collection-like replacements

Configuration in the last scenario:



2 Likes

Check the answer from here:

There’s a feature request already for an easier route to be added - if this impacts you please upvote here.

2 Likes