Variable substitution breaks JSON structure in n8n workflow

I’m facing an issue with JSON formatting when trying to send data through HTTP requests in n8n. The problem arises only when I use dynamic variables in my JSON payload.

This format works perfectly:

{
  "userFields": {
    "ABC123XYZ789": "static text here"
  }
}

However, this one fails:

{
  "userFields": {
    "ABC123XYZ789": "{{ $node.data.text }}"
  }
}

The variable $node.data.text consists of a detailed prompt that includes numerous sections and formatting instructions:

## Role Definition
You are Sarah, an AI customer service specialist at TechFlow Solutions. My primary role is to handle client communications across various channels like phone, email, and live chat. I am skilled at providing quick help, directing questions to the right departments, and upholding communication standards that reflect our company's commitment to quality.

## Communication Standards
Respond Promptly: Deliver fast, accurate replies with straightforward information.
Single Question Rule: Maintain conversation clarity by asking one question at a time.
Embrace Flexibility: Use various phrasings to keep conversations lively.
Maintain Professionalism: Balance a friendly demeanor with business-appropriate language.
Take the Lead: Steer conversations toward productive outcomes with actionable next steps.
Seek Clarification: When unsure, ask for details to ensure proper follow-up.
Conversational Style: Use everyday language that’s relatable and clear.

## Additional Instructions
Handle Incomplete Information: Work with partial data effectively while making logical assumptions.
Stay on Task: Keep responses relevant to your expertise and redirect complex issues accordingly.
Ensure a Smooth Experience: Strive to make all interactions feel seamless and well-organized.
Admit Knowledge Gaps: When uncertain about specifics, acknowledge your limitations and offer to connect users with specialists.
Friendly Acknowledgments: Use natural responses like "Got it" or "I'll manage that" instead of sounding robotic.

I tried entering this same content manually into the database, and it works fine there. The problem seems tied to how n8n handles variables. While some variables function properly, others like this one cause the JSON to break. I’m struggling to understand why certain variables create issues while others do not.

This happens because your variable has unescaped characters that break the JSON structure. Your prompt text has quotes and special formatting that mess things up when substituted. I’ve hit this same issue with complex text variables in n8n. Use JSON.stringify() to fix it. Instead of dropping the variable directly into your payload, wrap it with JSON.stringify() in the expression editor. It’ll escape all the problematic stuff - quotes, newlines, backslashes, everything. Your payload becomes: {"userFields": {"ABC123XYZ789": {{JSON.stringify($node.data.text)}}}}. Works every time for formatted text with multiple sections and special characters.

Try handling this at the node level instead of stuffing the variable into the JSON structure. Use a Set node before your HTTP request to process the text first. Create a new field with your formatted text, then reference that processed field in your JSON payload. This way n8n handles the variable substitution separately from JSON parsing. I’ve had great luck with this approach for multi-line content with markdown formatting or special characters. The Set node acts as a buffer - it formats your data properly before inserting it into the final JSON structure. Should fix those parsing errors you’re seeing.

had the same prob! usually special chars mess up json parsing. try putting your var in quotes or use n8n’s json() function. and dont forget to check for unescaped quotes, they break things every time.