Looking for 1 column to escape characters

I’m using Glide API to archive rows. I’d like to escape out all the offending characters with one JavaScript column instead of multiple Replace all columns, and I’m not very experienced with JavaScript yet.

For example, this little snippet that I got from a free AI tool :face_with_open_eyes_and_hand_over_mouth:

{
       return p1.replace(/\\/g, '\\\\') 
                      .replace(/"/g, '\\"')  
                      .replace(/\f/g, '\\f') 
                      .replace(/\n/g, '\\n') 
                      .replace(/\r/g, '\\r') 
                      .replace(/\t/g, '\\t'); 
}

does well at escaping, but converts some strings to dates which is not very helpful

What’s the simplest way to do this?

So that’s just a regular text column and a javascript column?

Yes - the javascript column on the right is producing the dates from “job 1” and “job 4”

That’s weird. I can’t reproduce your issue. I don’t know if there are other hidden characters that I’m missing, or my device is just handling it differently.

Not sure if this will work, but try this once.

{
    return p1.replace(/\\/g, '\\\\')
             .replace(/"/g, '\\"')
             .replace(/\f/g, '\\f')
             .replace(/\n/g, '\\n')
             .replace(/\r/g, '\\r')
             .replace(/\t/g, '\\t')
             .toString();
}

It returns the same results. I keep seeing this error in the JS columns, even when I create a new column, even before pasting in the code.

I’ll try playing around some more, but since I can’t reproduce, I don’t know if I’ll have an answer for you. If anything, you could try a different browser or computer to see if a different javascript engine makes a difference.

I don’t typically worry much about the warnings. They are just warnings, and many times due to one row that may have an empty or unexpected value. If they get in the way, then I’ll usually put it a Try/Catch to catch any errors and bow out gracefully. But I think Glide already does that and that’s why it only shows as a warning.

1 Like

I would take a step back here and look at the bigger picture.
When you talk about “offending” characters, I’m guessing that you are referring to characters that result in broken JSON when the JSON is constructed using a template?

If that’s the case, what you should be doing is using the JSON Object and/or JSON Template columns to construct your JSON. Using these columns will ensure that you always get valid JSON, without having to resort to any JavaScript gymnastics.

2 Likes

Thanks for the push to try those columns. After some trial and error, I got the JSON Template column to do the job. :slightly_smiling_face:

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.