Help with preparing HTML for JSON Call

#1

Does anyone have experience with using using Regex to Escape JSON strings?

I need to send a large amoutt of html code to a process via JSON. The process errors if I don’t Escape the HTML as an JSON String. I cannot figure out how to do this is Flowxo.

#2

I found some code that looks like it will do what i need online. Unfortunately I am not a coder and need some help with this. Can someone like @johnjackson show me how to format this code to run in FlowXO. I would really appreciate it.

public static String quote(String string) {
     if (string == null || string.length() == 0) {
         return "\"\"";
     }

     char         c = 0;
     int          i;
     int          len = string.length();
     StringBuilder sb = new StringBuilder(len + 4);
     String       t;

     sb.append('"');
     for (i = 0; i < len; i += 1) {
         c = string.charAt(i);
         switch (c) {
         case '\\':
         case '"':
             sb.append('\\');
             sb.append(c);
             break;
         case '/':
 //                if (b == '<') {
                 sb.append('\\');
//                }
             sb.append(c);
             break;
         case '\b':
             sb.append("\\b");
             break;
         case '\t':
             sb.append("\\t");
             break;
         case '\n':
             sb.append("\\n");
             break;
         case '\f':
             sb.append("\\f");
             break;
         case '\r':
            sb.append("\\r");
            break;
         default:
             if (c < ' ') {
                 t = "000" + Integer.toHexString(c);
                 sb.append("\\u" + t.substring(t.length() - 4));
             } else {
                 sb.append(c);
             }
         }
     }
     sb.append('"');
     return sb.toString();
 }
(John Jackson) #3

If quote() does what you need, then put the string you need to quote under the Inputs section of the code method, so you have the letter ‘x’ in the left box, and the string you want to quote in the right box.

Then, in the code box itself, below the function that you pasted in above and want to use:

resolve(quote(inputs.x));

So the idea is that the string you need to quote is held in inputs.x, and you call quote() with that value, and then the resolve() gives the result back to Flow XO.

Then the quoted result will be available as the output from the code service.

Hope that works for you!

#4

So i pasted this in the code section:

public static String quote(String string) {
if (string == null || string.length() == 0) {
return “”"";
}

 char         c = 0;
 int          i;
 int          len = string.length();
 StringBuilder sb = new StringBuilder(len + 4);
 String       t;

 sb.append('"');
 for (i = 0; i < len; i += 1) {
     c = string.charAt(i);
     switch (c) {
     case '\\':
     case '"':
         sb.append('\\');
         sb.append(c);
         break;
     case '/':

// if (b == ‘<’) {
sb.append(’\’);
// }
sb.append©;
break;
case ‘\b’:
sb.append("\b");
break;
case ‘\t’:
sb.append("\t");
break;
case ‘\n’:
sb.append("\n");
break;
case ‘\f’:
sb.append("\f");
break;
case ‘\r’:
sb.append("\r");
break;
default:
if (c < ’ ‘) {
t = “000” + Integer.toHexString©;
sb.append("\u" + t.substring(t.length() - 4));
} else {
sb.append©;
}
}
}
sb.append(’"’);
return sb.toString();
}

}
resolve(sb.append(inputs.x))

and x as my input and it doesn’t work

(John Jackson) #5

I think you’ll need:

resolve(quote(inputs.x));

Did you try that?

#6

I get this error:

Script Error: SyntaxError: Unexpected strict mode reserved word