Object spread operator in expression

I have incoming data in this format:

{"id": 123, "data": {"name": "example","value": 456}}

I have a node where I need to unroll the data object and post this as a body:

{"id": 123, "name": "example","value": 456}

The contents of data object are dynamic, so I can’t reference the individual fields. The natural solution would seem to be using the object spread operator in an expression, but that gives me a syntax error:

So how do I accomplish this?

After some experimenting I’ve found this is possible using a jmespath merge expression, but it’s pretty clunky and hard to read so if anyone has a better solution I’m listening!

{{ $jmespath($(‘Is record valid?’).item.json,“merge({id:id},data)”) }}

Hey @cacl-dis hope all is good.

Here is another way to flatten your object

But if you really want it unclunked, you are looking at a properly implemented flattener.

2 Likes

You need to put these commands inside a single expression.

It’s hard for me to type solution on the phone, I can suggest checking examples here: n8n Expressions Cheat-Sheet | n8n Arena: Creators Leaderboard

This is an n8n expressions mega cheat sheet I’ve created some time ago. There are definitely examples with triple dots.

1 Like

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