Unexpected Quotation Mark Injection in Set Node (Expressions)

Describe the problem/error/question

When using the Set node for for saving needed fields with JSON mode and expression interpolation (e.g., {{ $json.caption }}), I’m seeing an issue where unexpected quotation marks are injected into the output, even when the original input does not contain them.

This happens most commonly when the caption contains a colon. For example, the word realize in the example below in the sentence gets surrounded by quotes automatically, making the JSON invalid.

I’ve tried hardcording it with JS code, like turnign it into string, and removign the quotation marks, but it still keeps on placing them there.

I believe this is a bug in how n8n handles templated strings in JSON mode inside the SET node.

What is the error message (if any)?

The ‘JSON Output’ in item 0 contains invalid JSON

Please share your workflow

I uploaded the iamge of the output, we can see the original input somewhere in the middle of the output JSON file, which has no issues, but the top part, unexpectedly adds quotation marks in different areas such as:

  1. Input JSON: “caption”: “Everyone takes different amounts of time to realize: no one is coming to save you. and many never figure it out until it’s too late.”
    Output JSON: “caption”: “Everyone takes different amounts of time to “realize”: no one is coming to save you. and many never figure it out until it’s too late.”

  2. Input JSON: “postedAt”: “2025-04-19T15:55:00.000Z”,
    Output JSON: “postedAt”: “2025-04-“19T15”:“55”:00.000Z”,

  3. Post Url does the same and Video URL does the same (not going to put it here as they are too long)

Share the output returned by the last node

{
  "username": "hormozi",
  "fullName": "Alex Hormozi",
  "caption": "Everyone takes different amounts of time to realize: no one is coming to save you. and many never figure it out until it’s too late.",
  "hashtags": "[]",
  "likes": 6529,
  "comments": 150,
  "topComment": "Your destiny is in your hands💪🏾",
  "videoViews": 48007,
  "videoPlays": 231023,
  "videoDurationSec": 5,
  "postedAt": "2025-04-19T15:55:00.000Z",
  "postUrl": "https://www.instagram.com/p/DIokonBpRzG/",
  "thumbnailUrl": "https://instagram.fgru6-1.fna.fbcdn.net/v/t51.2885-15/491041528_18275132860250343_3966867281092633759_n.jpg?stp=dst-jpg_e35_p1080x1080_sh0.08_tt6&_nc_ht=instagram.fgru6-1.fna.fbcdn.net&_nc_cat=103&_nc_oc=Q6cZ2QHz59t_zLOquyxZotFT4yVrPjSKb5LOiGKh6h_Im0ZLpXHdta_Qal8WailTGK_95vA&_nc_ohc=sPxf3FblzosQ7kNvwG9D9Yw&_nc_gid=mkjwnl-INiK8nPsiCCfc6w&edm=APs17CUBAAAA&ccb=7-5&oh=00_AfFtCVGLoScudMaX93RYGsgkcLbzgqFKWraXzXuweo4-7A&oe=6809E693&_nc_sid=10d13b",
  "videoUrl": "https://instagram.fgru6-1.fna.fbcdn.net/o1/v/t16/f2/m86/AQMS_ASHCXdDL050PJgrQekm0BQsp4Yn4dwd624ioh3eFvK1mqDWXRl7tVMYUz8wsQrnHIa5PPz7j5xfvmXxuxsnM8kQv_RWmYm5ZEw.mp4?stp=dst-mp4&efg=eyJxZV9ncm91cHMiOiJbXCJpZ193ZWJfZGVsaXZlcnlfdnRzX290ZlwiXSIsInZlbmNvZGVfdGFnIjoidnRzX3ZvZF91cmxnZW4uY2xpcHMuYzIuNzIwLmJhc2VsaW5lIn0&_nc_cat=109&vs=1635664740452540_3683779682&_nc_vs=HBkcFQIYUmlnX3hwdl9yZWVsc19wZXJtYW5lbnRfc3JfcHJvZC80RjQ5MzBBRkJCNzlEOEVDQTZENEJDN0JERUU3REZBQl92aWRlb19kYXNoaW5pdC5tcDQVAALIAQAoABgAGwAVAAAmmPCb4qHBzT8VAigCQzMsF0AUAAAAAAAAGBJkYXNoX2Jhc2VsaW5lXzFfdjERAHX%2BBwA%3D&_nc_rid=085af8707a&ccb=9-4&oh=00_AfF2Zu4v4xjZoEAWerhNAaNlaquTzZaTcXxECM-Ov4yg9Q&oe=68062098&_nc_sid=10d13b"
}

Should be whatever the Original output is.

Information on your n8n setup

  • **n8n version: ** 1.84.1
  • **Database (default: SQLite): ** Excel currently
  • **n8n EXECUTIONS_PROCESS setting (default: own, main): ** main
  • **Running n8n via (Docker, npm, n8n cloud, desktop app): ** n8n Cloud
  • **Operating system:**n8n Cloud (Managed, Linux-based)

Hi @George_Babakhanov Welcome to n8n :n8n: community :tada:

Please try use JSON.stringify() in your expression,

for example:

"caption": {{ JSON.stringify( $json.caption) }}

If this answers your question, please mark the reply as the solution ✅🙏🏻
1 Like

On it’s own it did work. But when I tried to use it all together with all the other parameters it again gave me the same issue.

Would I have to put “JSON.stringify” into every line?

Yes, you’ll want to use JSON.stringify on any field that might include special characters like \n

To be safe, it’s a good idea to apply it to all string values…

{
“username”: “{{$json.ownerUsername}}”,
“fullName”: “{{$json.ownerFullName}}”,
“caption”: {{ JSON.stringify( $json.caption)}}
“topComment”: {{ JSON.stringify($json.latestComments[0]?.text)}},
}

i was testing it one by one. up to caption everything worked. TopComment didnt work. is there a reason?


I even tried it without JSON.stringify and gives same thing.

you missed ,

try this:

{
"username": "{{ $json.ownerUsername }}",
"fullName": "{{ $json.ownerFullName }}",
"caption": {{ JSON.stringify( $json.caption ) }},
"topComment": {{ JSON.stringify( $json.latestComments[0]?.text ) }}
}

{
“username”: “{{ $json.ownerUsername }}”,
“fullName”: “{{ $json.ownerFullName }}”,
“caption”: {{ JSON.stringify( $json.caption ) }},
“topComment”: {{ JSON.stringify( $json.latestComments[0]?.text ) }},
“hashtags”: {{JSON.stringify($json.hashtags)}},
“comments”: {{JSON.stringify($json.commentsCount)}},
}

Sorry took too long. But I did miss the comma. But now I got to the poiont where the numbers need to be imprinted and its doing the same thing.

Thank you for the help by the way. Trully appreciate it!

1 Like

No worries at all, glad to hear you’re making progress!

Let me know if you hit any more issues!

{
“username”: “{{ $json.ownerUsername }}”,
“fullName”: “{{ $json.ownerFullName }}”,
“caption”: {{ JSON.stringify( $json.caption ) }},
“topComment”: {{ JSON.stringify( $json.latestComments[0]?.text ) }},
“hashtags”: {{JSON.stringify($json.hashtags)}},
“comments”: {{JSON.stringify($json.commentsCount)}},
}

The only problem Im getting now, is the Comments its a number and again I tried to use the stringify and not stringify and it gave an error, i dont knwo if its because its an integer or something else. This time I didnt miss the comme haha

1 Like

If the “Comments” is a number, do not use JSON.stringify, just use:

"comments": {{ $json.commentsCount }}

{
“username”: “{{$json.ownerUsername}}”,
“fullName”: “{{$json.ownerFullName}}”,
“caption”: {{ JSON.stringify( $json.caption ) }},
“topComment”: {{ JSON.stringify( $json.latestComments[0]?.text ) }},
“hashtags”: {{JSON.stringify($json.hashtags)}},
“comments”: {{$json.commentsCount}},
“videoViews”: {{ $json.videoViewCount }},
“videoPlays”: {{ $json.videoPlayCount }},
“videoDurationSec”: {{ $json.videoDuration }},
“postedAt”: “{{JSON.stringify($json.timestamp)}}”
}

last little bits. This one I don’t really know what to use (this is for PostedAt): “2025-04-19T15:55:00.000Z”.
It has numbers and letters. I’m guessing turn it fully into string, but stringify didn’t work.

Also quick question on the side, would I have to use quotation marks when usign Json.stringify? Because I see the output already puts them into quotation marks makign it a string. (just asking to not have a headache in the future)

1 Like

Solved it by not using Stringify, just used quotations around it and it worked.

1 Like

Everything worked.
@mohamed3nan Thank you for the help. Will click solved now!

1 Like

Glad it worked! I made this for you, just a simpler approach, in case you need it.

2 Likes

That was proabably a smarter way of doing it, just manually mapping it HAHA. I just love making my life difficult!

Thank you again!

1 Like

No worries :smiley:
Difficult things will definitely teach you something new😉

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