Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...


Code Block
do(
    $value = $args[1],
    $value = replaceAll( $value , "\b","\\\\b"),
    $value = replaceAll( $value , "\f","\\\\f"),
    $value = replaceAll( $value , "\t","\\\\t"),
    $value = replaceAll( $value , "\r","\\\\r"),
    $value = replaceAll( $value , "\n","\\\\n"),
    $value = replaceAll( $value , "\\\\","\\\\\\\\"),
    $value = replaceAll( $value , '"','\\\\"')
)


JSON Un-escape Macro. This is useful when importing JSON into PhixFlow from an external source. It converts an escaped sequence (eg \\\\t) back into a usable character. The double \\ ahead of the \t in the output is necessary to escape \t when the jep expression is passed and will be converted to simply \t (the tab character). 


Code Block
do(
    $value= $args[1],
   
    $value = replaceAll($value, "\\\\t","\\\t"),
    $value = replaceAll($value, "\\\\b","\\\b"),
    $value = replaceAll($value, "\\\\r","\\\r"),
    $value = replaceAll($value, "\\\\n","\\\n"),
    $value = replaceAll($value, "\\\\f","\\\f"),
    $value
)


Tip

Check the format of the JSON you are importing to see what adjustments are required. Sometimes characters such as \\\\\\\\" may be helpfully un-escaped prior to import, making it unnecessary to unescape every special character. 

Validating JSON Files

JSON code can include characters that cannot be written to a database that is using the UTF-8 character set. For example, 4-bit Emojis are not supported by UTF-8.

The following regular expression lists the characters expected in a JSON file and removes invalid characters from it.

Code Block
replaceAll(JSON Code,"[^\\p{Space}0-9A-Za-z!:\\\"%&\\[*()\\],-/_\\\\{}\\.]","")

The backslash character escapes special characters such as [] , . and \ 

Tip

The UTF-8mb4 character set supports a wider range of characters, such as Emojis. Migrating your database to use this character set would allow JSON to be used without needing to validate it using a regular expression.

See also: Regular Expressions


...