Variable substitution breaks JSON structure in n8n workflow

I’m having trouble sending data to Go High Level via HTTP request in n8n. The JSON payload works perfectly when I use static values:

{
  "customData": {
    "field_ABC123XYZ789": "static text here"
  }
}

However, when I try to use dynamic variables, the request fails:

{
  "customData": {
    "field_ABC123XYZ789": "{{ $json.response.text }}"
  }
}

The variable $json.response.text contains a long AI prompt with multiple paragraphs, special characters, and formatting instructions. When I test the same content by manually inserting it into the database, it works fine.

I’ve tested with shorter variables and they work correctly. The issue seems to occur randomly with certain variables, but I can’t figure out the pattern. Has anyone experienced similar problems with JSON formatting when using variables in n8n HTTP requests?

try wrapping your variable in quotes properly or use the code node to sanitize the text first. i had similar weirdness with long strings containing newlines and it was messin up the json parsing. sometimes switching from json to form-data helps too if go high level accepts it

This sounds like a JSON escaping issue that I’ve run into before. When your AI prompt contains characters like quotes, backslashes, or newlines, they’re probably breaking the JSON structure during variable substitution. N8n doesn’t always handle complex text data properly when inserting it directly into JSON strings. Try using the expression editor instead of the JSON view. Switch to the expression tab for that field and enter your variable there - n8n handles escaping better in expression mode. Alternatively, you can use the JSON.stringify() function around your variable to properly escape special characters before insertion. I had the exact same problem when passing long form data with mixed content types. The solution was either using expressions or preprocessing the text to escape problematic characters beforehand.

I encountered this exact behavior when working with AI-generated content in n8n workflows. The root cause is usually unescaped characters in your text that break JSON parsing. What worked for me was using n8n’s built-in text processing functions before the HTTP request node. Add a Set node between your data source and the HTTP request, then use the .replace() method to handle problematic characters. Something like {{ $json.response.text.replace(/"/g, ‘"’).replace(/
/g, ‘\n’) }} should clean up quotes and newlines. You can also try setting the Content-Type header explicitly to ‘application/json’ in your HTTP node configuration. Another approach that saved me time was using the HTTP Request node’s body type as ‘Form-Data’ instead of JSON when possible, since it handles special characters more gracefully. Test with a simple console.log in a Code node first to see exactly what characters are causing the parsing to fail.