Skip to content
Home » Fix JSON Control Characters in Bloomreach Push

Fix JSON Control Characters in Bloomreach Push

    Fix JSON Control Characters in Bloomreach Push

    The “Invalid JSON: Invalid control character” error occurs when dynamic snippets or Jinja parameters inject raw newlines, carriage returns, or unescaped double quotes directly into quoted JSON payload strings, violating RFC 4627 JSON specifications.

    How mature is your CRM setup?

    Find out in minutes with a free CRM maturity check.

    Get Your Free Check

    What Causes Invalid JSON Control Character Errors in Push Payloads

    JSON specifications prohibit unescaped control characters (ASCII 0 to 31) inside quoted string values, including newlines (\n), carriage returns (\r), and tabs. When Bloomreach renders a push notification payload, the final JSON must conform to these rules. A valid JSON string containing a newline must encode it as the two-character sequence \n, not as an actual line break.

    If a snippet or Jinja tag outputs text containing raw line breaks, the resulting JSON becomes invalid and the push campaign fails to send. The error message “Invalid JSON: Invalid control character” indicates that Bloomreach detected a prohibited character inside a quoted string during payload validation.

    How Unescaped Line Breaks in Snippets Break JSON Parsing

    When a customer attribute or snippet parameter contains multi-line text, Bloomreach injects that raw text into the JSON payload at send time. A JSON string value like "body": "Line one\nLine two" is invalid because the actual newline character breaks the string boundary. The JSON parser encounters the line break inside the quoted value and stops parsing, treating the remainder as syntax error rather than content.

    This happens because JSON treats the newline as the end of the string literal, leaving unparseable text after it. For example, if a customer’s description field contains a line break, and you render it directly as "description": "{{ customer.description }}", the payload becomes malformed if that field spans multiple lines in the source data.

    Escaping Newlines and Carriage Returns in Jinja Snippets

    Use the Jinja replace filter to convert raw line breaks into escaped sequences before rendering inside JSON strings. Apply the filter to the snippet output or customer attribute:

    {{ snippet('my_snippet') | replace('\n', '\\n') | replace('\r', '\\r') }}

    This converts actual newline characters to the literal text \n and \r, which are valid inside JSON strings. Chain multiple replace filters to handle both newlines and carriage returns in sequence. The syntax '\n' matches an actual newline character in the input, while '\\n' produces the two-character output sequence. If your customer data contains both types of line breaks, apply both filters. You can also combine them into a single variable assignment for reuse across multiple fields:

    {% set safe_text = customer.bio | replace('\n', '\\n') | replace('\r', '\\r') %}
    {{ safe_text }}

    Handling Double Quotes and Special Characters in Dynamic Text

    Customer profile attributes may contain unescaped double quotes, which terminate the JSON string prematurely. Escape these before passing them into snippets or rendering directly in the payload:

    {% set clean_text = customer.description | replace('"', '\\"') %}
    {{ snippet('my_snippet', {'text': clean_text}) }}

    Backslashes also require escaping in JSON: replace('\\', '\\\\'). Apply quote and backslash escaping before newline escaping to avoid double-escaping issues. For example, if a customer’s name contains both a backslash and a quote, escape the backslash first, then the quote. The order matters because escaping the quote first would create a backslash that then needs escaping. A comprehensive sanitization chain might look like:

    {% set safe_name = customer.name | replace('\\', '\\\\') | replace('"', '\\"') | replace('\n', '\\n') %}

    Choosing Between Stripping and Replacing Control Characters

    Two approaches exist: strip newlines entirely using replace('\n', ' ') to flatten text into a single line, or escape them as replace('\n', '\\n') to preserve line breaks in the rendered message.

    Stripping is simpler and safer for push notification body text, which typically displays on a single line or wraps automatically. Escaping preserves intentional line breaks if your push template uses multi-line formatting or if the JSON payload structure requires embedded line breaks.

    Based on Voxwise implementation experience, stripping newlines is more reliable for avoiding JSON errors across diverse customer data sources, since it requires no assumptions about data quality or the intent behind line breaks in source attributes.

    Stripping also reduces the risk of rendering errors in downstream systems that parse the JSON payload. However, if your push notification design intentionally uses newlines for formatting, escaping preserves that structure while maintaining JSON validity.

    Enforcing Single Quote Syntax in Bloomreach Jinja Tags

    Bloomreach push notifications require single quotes for all Jinja string parameters. Double quotes are not supported and will cause errors. Use this syntax:

    {{ snippet('my_snippet', {'key': 'value'}) }}

    Not this:

    {{ snippet("my_snippet", {"key": "value"}) }}

    This rule applies to all Jinja tags in push notification templates, including conditional blocks and variable assignments. Single quotes protect the Jinja parameter values from being interpreted as JSON delimiters.

    When you pass customer data into a snippet using the parameter syntax, always wrap the parameter name and value in single quotes, not double quotes. This prevents the Jinja parser from confusing your parameter syntax with the JSON structure of the push payload itself.

    Validating Rendered Push Payloads in Bloomreach Previews

    Inspect the actual JSON payload rendered by Bloomreach before sending. In the push campaign editor, select a test customer and view the preview. The preview displays the final rendered content, including all Jinja expansions and snippet output.

    Compare the preview output against the JSON source code to verify that newlines, quotes, and special characters have been properly escaped. If the preview shows unescaped control characters or malformed text, revise the Jinja filters in the template and re-test.

    You can also copy the rendered JSON from the preview and validate it using external JSON validation tools to confirm compliance with RFC 4627 before launching the campaign. Testing with a customer record that contains edge cases—names with quotes, descriptions with line breaks, or special characters—will expose escaping gaps that might not appear with typical customer data.

    Sanitization RequirementJinja Filter SyntaxRendered Output EffectPrimary Use Case
    Escape Newlines for JSON\| replace('\n', '\\n')Converts \n to literal \n in JSONPreserve line breaks in push body
    Strip Newlines Entirely\| replace('\n', ' ')Removes line breaks, replaces with spaceFlatten multi-line attributes
    Escape Double Quotes\| replace('"', '\\"')Converts " to \" in JSONProtect customer text with quotes
    Trim Whitespace\| trimRemoves leading/trailing spacesClean extra whitespace after escaping

    FAQ

    What happens if a customer attribute contains raw tab characters in a push payload?

    Tab characters (ASCII 9) are control characters and break JSON validity the same way newlines do. Escape them with replace('\t', '\\t') or strip them with replace('\t', ' ') before rendering in the payload. Include tab escaping in your comprehensive sanitization chain if your customer data sources might contain tabs.

    Why do double quotes inside Jinja parameters trigger push campaign errors?

    Double quotes inside a Jinja parameter value terminate the JSON string prematurely. For example, "body": "customer said "hello"" breaks because the second quote ends the string early. Escape them as \" using the replace filter before passing to snippets. Bloomreach requires single quotes for all Jinja parameters, which helps avoid this issue, but customer data passed through those parameters still needs quote escaping.

    How do you debug JSON errors in mobile push payloads versus browser push?

    Both mobile and browser push use identical JSON validation rules. Use the Bloomreach preview feature for both channels to inspect the rendered payload. The error message will indicate which field contains the invalid control character. If the error persists across preview tests, check whether your sanitization filters are applied to all dynamic content, not just the primary message body.

    Can snippets be sanitized at the source snippet level instead of in push templates?

    Yes, you can apply replace filters inside the snippet definition itself, but sanitizing at the template level is more flexible. It allows different escaping rules for different campaigns and makes the sanitization visible to template editors. Centralizing sanitization in the push template also prevents accidental bypassing of escaping rules if a snippet is reused in other channels where escaping rules differ.

    Get Your Push Templates Validated

    Unescaped control characters in Jinja snippets cause push campaigns to fail silently for affected customers.

    Audit your Bloomreach push templates and snippet architecture with Voxwise to identify escaping gaps, implement proper sanitization filters, and test payloads before launch.

    Tags: