Postgres Insert Node - Object field fails with newline characters (\n)

Hi, I’m facing a issue when inserting objects into Postgres using the Postgres Insert Node.

Setup

  • Postgres column type: jsonb (expects an object)

  • n8n version: 1.108.1 (Self Hosted)

  • Node: Postgres Insert (v2.6)

What I’m trying to do

I want to store user messages in the body column as an object like this:
{ “message”: “some words” }

If the message has no newline characters, this expression works fine inside the Postgres Insert node and it saves correctly:
{ “message”: $(‘Prepare Data for AI’).item.json.body.message }

but if the input contains a newline, then I get this error:

Invalid input for ‘body’ \[item 0\]
‘body’ expects a object but we got ‘{ “message”: “some\\nwords” }’

It looks like n8n is treating the whole thing as a string, not an object, whenever newlines are present.
I even tried syntax like this: ={{ { “message”: $(‘Prepare Data for AI’).item.json.body.message } }}
But then I get [object Object]

Workaround

I solved it by using a Function node before Postgres:

const message = {
  body: {
    message: $('Prepare Data for AI').item.json.body.message
  }
}
return message

And then in Postgres Insert: {{ $json.body }}

I have tried this in other situation and it works just fine. But I wonder if its a best solution.

So my question is:

Is this expected behavior?

  • Is there a proper syntax to directly map an object into Postgres Insert without having to use a Function node?
  • Or is this a bug with expression parsing when objects contain \n characters?

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