Issue inserting json array into postgres jsonb column

Describe the problem/error/question

I’m trying to store some response data into a Postgres Table. I’ve got a few fields where some of the types are arrays (empty or not empty):

{"field_1" : [], "field_2": [] }

I was originally attempting to store these data using jsonb, but it appears that when mapping the fields from the response JSON to the postgres insert node, I’m getting an error that an Object is expected and not an array.

I attempted to do a workaround I found here that says using JSON.stringify() on the object is supposed to help, but that did not work.

I’ve also tried changing the postgres column datatype to jsonb[]. Again, same issue.

I feel as though this is a valid means to set the data type into postgres jsonb, so I’m unsure why this is throwing an error. Any help would be much appreciated.

What is the error message (if any)?

Invalid input for ‘mentions’ [item 0]

‘mentions’ expects a object but we got array

Please share your workflow

I’m going to hold off until I figure how to keep some PII off this.

Share the output returned by the last node

Information on your n8n setup

  • **n8n version:1.75.2
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • **Running n8n via (Docker, npm, n8n cloud, desktop app):Docker
  • Operating system:

If you’re going to send an empty array, try using null instead. See if that works.

Make sure your column is configured to accept null values.

Hey thanks for the reply! So after some more testing… null works fine. I used a Set Node to conditionally set all the length === 0 arrays to null and that worked with no issues.

Seems like now that the issues is that the Postgres JSONB type wants an object. I’ve tried to send both the array empty as well as Stringify the array. The only other thing I could mabye do is set the array as the field in an object, so something like {"field1": [] }{ "field1": { "field1" : [] } }… which feels incredibly silly to have to do.

Any thoughts?

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