Create Object in Set Node

Describe the problem/error/question

I’m trying to create a simple dictionary (object) lookup. For instance

{
  "Apple": { "color": "blue", "inventory": 2 },
  "Banana": { "color": "Yellow", "inventory": 0 }
}

In my set node I select “object” and include this statement withing double parentheses {{ }}

{{
  {
    "Apple": { "color": "blue", "inventory": 2 },
    "Banana": { "color": "Yellow", "inventory": 0 }
  }
}}

What is the error message (if any)?

'dict' expects a object but we got '[object Object] ' [item 0]
To fix the error try to change the type for the field "dict" or activate the option “Ignore Type Conversion Errors” to apply a less strict type validation

Please share your workflow

Share the output returned by the last node

None

Information on your n8n setup

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

For now I will just switch to JSON or the code node. But the manual field option should also work in my opinion. Also the AI suggestions on how to fix it are pretty bad.

You do not need {{ }} around your json. remove them and your JSON would be valid

1 Like

This case is for manual mapping. In case it’s JSON I can just type JSON indeed.

The above is your workflow. i just removed {{ }} and it works now.

if you want to use a variable in that json do it like below

{
  "Apple": {
    "color": {{ Math.random() < 0.5 ? "red" : "blue"}},
    "inventory": 2
  },
  "Banana": {
    "color": "Yellow",
    "inventory": 0
  }
}

You can use Mode=json too

thanks. this is so odd, I didn’t change the “code” and ran the workflow again without error. There must’ve been something else causing the errors.