Skip to content

How to fix com.amazon.coral.service#serializationException Error

0

My Api Gateway endpoint is failing with the above error. The endpoint is integrated with a SF. The root cause is my name field. This field is an array of names, and it looks like so; name: ["Sam Jay O'Donnell"] The problem is the apostrophe in the name; without the apostrophe, everything works fine. The request is not reaching the SF, and if I execute the SF directly with a name having an apostrophe, the SF executes successfully. I saw the error in AWS console when I tested the API. Initially, I have the below in the req parameter in CF,

"input": "$util.escapeJavaScript($input.json('$')):

then I changed it to

"input": $input.json('$)

I still got the error afterward, any idea how to fix this serialization issue?

1 Answer
1
Accepted Answer

The issue you're experiencing with the apostrophe in "O'Donnell" causing a serialization exception is related to how special characters are handled in JSON processing between API Gateway and Step Functions.

In the Amazon States Language, certain characters are reserved for Intrinsic Functions and must be escaped, including the single quote ('). When these characters appear in your data and aren't properly escaped, they can cause serialization errors.

To fix this issue, try the following solution in your API Gateway integration request mapping template:

"input": "$util.escapeJavaScript($input.json('
        The issue you're experiencing with the apostrophe in "O'Donnell" causing a serialization exception is related to how special characters are handled in JSON processing between API Gateway and Step Functions.

        In the Amazon States Language, certain characters are reserved for Intrinsic Functions and must be escaped, including the single quote ('). When these characters appear in your data and aren't properly escaped, they can cause serialization errors.

        To fix this issue, try the following solution in your API Gateway integration request mapping template:

        ```
        "input": "$util.escapeJavaScript($input.json('$')).replaceAll("\\'", "'")"
        ```

        This solution uses `escapeJavaScript` to properly escape the JSON, then specifically replaces the escaped single quotes (`\'`) with regular single quotes (`'`). This prevents the serialization error while maintaining the integrity of your data.

        If you're still experiencing issues, you might need to handle newlines as well:

        ```
        "input": "$util.escapeJavaScript($input.json('$')).replaceAll("\n", "").replaceAll("\\'", "'")"
        ```

        This approach removes any newlines in the payload and then handles the single quote escaping, which has been found to resolve similar serialization issues when passing data with apostrophes from API Gateway to Step Functions.
    #39;)).replaceAll("\\'", "'")"

This solution uses escapeJavaScript to properly escape the JSON, then specifically replaces the escaped single quotes (\') with regular single quotes ('). This prevents the serialization error while maintaining the integrity of your data.

If you're still experiencing issues, you might need to handle newlines as well:

"input": "$util.escapeJavaScript($input.json('
        The issue you're experiencing with the apostrophe in "O'Donnell" causing a serialization exception is related to how special characters are handled in JSON processing between API Gateway and Step Functions.

        In the Amazon States Language, certain characters are reserved for Intrinsic Functions and must be escaped, including the single quote ('). When these characters appear in your data and aren't properly escaped, they can cause serialization errors.

        To fix this issue, try the following solution in your API Gateway integration request mapping template:

        ```
        "input": "$util.escapeJavaScript($input.json('$')).replaceAll("\\'", "'")"
        ```

        This solution uses `escapeJavaScript` to properly escape the JSON, then specifically replaces the escaped single quotes (`\'`) with regular single quotes (`'`). This prevents the serialization error while maintaining the integrity of your data.

        If you're still experiencing issues, you might need to handle newlines as well:

        ```
        "input": "$util.escapeJavaScript($input.json('$')).replaceAll("\n", "").replaceAll("\\'", "'")"
        ```

        This approach removes any newlines in the payload and then handles the single quote escaping, which has been found to resolve similar serialization issues when passing data with apostrophes from API Gateway to Step Functions.
    #39;)).replaceAll("\n", "").replaceAll("\\'", "'")"

This approach removes any newlines in the payload and then handles the single quote escaping, which has been found to resolve similar serialization issues when passing data with apostrophes from API Gateway to Step Functions.
Sources
Amazon States Language
Event Bridge Rule and Single-Quote Escaping | AWS re:Post

answered a year ago

EXPERT

reviewed a year ago

  • Thank you so much, your solution worked.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.