ComfyUI JSON Error when using dynamic prompt with {{ $json.chatInput }}

Hi everyone,

I’m currently trying to integrate ComfyUI into my n8n workflow using the n8n-nodes-comfyui node.

I’m following some community tutorials that suggest replacing a static prompt with {{ $json.chatInput }} so I can dynamically inject prompts from earlier nodes. However, when I try this, I get the following error:
ComfyUI API Error: Bad escaped character in JSON at position 517

Here’s a simplified version of the JSON I’m working with:
“6”: {
“inputs”: {
“text”: “{{ $json.chatInput }}”,
“speak_and_recognation”: {
value”: [
false,
true
]
}
}
}

When I don’t modify the prompt and use static text instead, everything works perfectly.

I’m not sure if this is an escaping issue or something else.
Does anyone know the correct way to inject a dynamic value like this into the JSON payload?

For context:

  • n8n version: 1.93.0
  • n8n-nodes-comfyui version: 0.0.6

Any help would be greatly appreciated. Thank you in advance!

This could probably be an issue with the API. The data may not be transferring properly. Are you sure the JSON payload outside the dynamic part is formatted correctly? Make sure of that first.

Try to use expressions outside the JSON string. The error usually happens because the template {{ $json.chatInput }} is inside a JSON string, which expects plain text, but your input might have quotes or special characters that break the JSON format.

Like, “text”: {{ $json.chatInput }},

Also chekc your dynamic text. If your dynamic text (chatInput) contain quotes, backslashes, or other special JSON characters, you need to escape them properly. You can do this by using a JavaScript function in the expression to escape it, something like: “text”: “{{ $json.chatInput.replace(/\/g, ‘\\’).replace(/"/g, '\”') }}",

Hope this helps, cheers!

Hello,

Thank you for your response :heart_eyes:

In my current setup, I have the following configuration:
“6”: {
“inputs”: {
“text”: “a lady”,
“speak_and_recognation”: {
value”: [
false,
true
]
}
}
}

This configuration works correctly and generates the desired image.

However, when I modify the configuration to use a dynamic prompt:
“6”: {
“inputs”: {
“text”: “{{ $json.chatInput }}”,
“speak_and_recognation”: {
value”: [
false,
true
]
}
}
}

The workflow fails to execute.

I followed the steps outlined in this tutorial:

Hi @BearDD,

This is a common issue,

Could you please try to use JSON.stringify() like this:

"text": {{ JSON.stringify( $json.chatInput ) }}


Hello, thank you for your reply. :pray:

I tried your suggestion of using {{ JSON.stringify($json.chatInput) }},
but unfortunately, I’m still getting the same error.

Hi @BearDD

Please use it without quotes, as JSON.stringify() automatically adds the quotation marks " "

just like this:

"text": {{ JSON.stringify( $json.chatInput ) }}


Thank you again for your reply. :pray:

I gave it a try, but unfortunately the error is still the same.

I also rewatched the tutorial video I mentioned earlier.
It seems that the original setup in the video still includes the quotation marks (" "),
but it works fine and successfully generates the image. :tired_face:


If I don’t use the JSON input and enter the prompt directly, it works fine and the image is generated successfully.

Hi everyone,
I finally figured it out!

I had previously tried downgrading the n8n version, but that didn’t help.

My original workflow was:
Chat trigger → ComfyUI node
(This setup was also what the tutorial video I watched used.)

However, I kept running into issues when trying to modify the JSON directly inside the ComfyUI node.

So I decided to try something different:
I added a Set node before the ComfyUI node, pasted the JSON that ComfyUI uses into it, made the necessary changes to chat_input inside the Set node, and then passed the entire JSON to the ComfyUI node.

And… it worked!! :tada:
I still don’t really understand why this workaround is necessary—it feels a bit redundant—but hey, at least it’s working now!

Huge thanks to the two awesome folks who shared their solutions earlier :raised_hands:
It’s been super fun and helpful chatting with everyone here!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.