Escape special characters in source data

This example demonstrates how to use the Mapper Snap to escape special characters in source data so that downstream Snaps can interpret them correctly.


JSON Generator pipeline

Download this pipeline
  1. Generate the input data
    Add a JSON Generator Snap and enter the custom JSON data containing special characters, such as a single quote ('), in the required fields.

    JSON File in JSON Generator Snap

  2. Preview the output

    JSON Generator Output

    Click the SL-data-preview icon to view the generated data. The special characters appear as entered in the raw output.
  3. Add a Mapper Snap to escape characters
    Insert a Mapper Snap after the JSON Generator Snap. Use the Expression field to escape special characters based on the requirements of the downstream system.

    Choose the appropriate escape expression. Examples include:

  4. Validate the output
    Validate the pipeline to ensure the escape characters are added correctly. Review the output preview of the Mapper Snap to confirm the updated data.
    Escape Character Expression Sample Output
    Single Quote (')
    $original.mapValues((value,key)=> value.toString().replaceAll("'","''"))

    OR

    $original.mapValues((value,key)=> value.toString().replaceAll("'","\''"))

    CSV:

    $[' Business-Name'].replace ("'","''")
    Mapper Output 1
    Ampersand (&)
    $original.mapValues((value,key)=> value.toString().replaceAll("'","\&'"))

    OR

    $original.mapValues((value,key)=> value.toString().replaceAll("'","&'"))

    CSV:

    $[' Business-Name'].replace ("'","&'")
    Mapper Output 2
    Backslash (\)
    $original.mapValues((value,key)=> value.toString().replaceAll("'","\\'"))
    Note: Backslash is configured as an escape character in SnapLogic®. Therefore, it must itself be escaped to be displayed as text.

    CSV:

    $[' Business-Name'].replace ("'","\\'")
    Mapper Output 3

The data with escaped characters is now ready to be consumed by downstream Snaps without interpretation issues. Customize the expression as needed for your target endpoint's supported escape characters.

Learn more in Escaping Single Quotes in Error Pipeline - Issue.