How to extract array from JSON object in N8N workflow?

I’m working on an N8N workflow and I’m stuck. My current output is a JSON object with a ‘data’ key containing an array. I want to get rid of the ‘data’ key and just return the array directly. Here’s what I have now:

{
  "data": [
    {
      "recordId": 9876
    }
  ]
}

And this is what I’m aiming for:

[
  {
    "recordId": 9876
  }
]

I’ve tried tweaking the item list and using split output options, but it didn’t work. Any suggestions on how to eliminate the outer object and return just the array? Thanks in advance for your help!

hey there! have you tried the ‘item lists’ node? it can be super helpful for this kinda thing. Just set it to ‘Aggregate’ mode and use ‘={{$json.data}}’ as the Value. that should give you just the array without the ‘data’ key. let me know if it works for ya!

For this scenario, I’d recommend using the ‘JSON Parse’ node in N8N. It’s quite effective for manipulating JSON structures. Set the ‘JSON Input’ to ‘={{$json}}’ and the ‘Property Name’ to ‘data’. This should parse your input and output just the array content.

If that doesn’t yield the desired result, another approach is to use the ‘Function’ node with a bit of JavaScript. You could write something like:

return [{ json: $input.all()[0].json.data[0] }];

This extracts the first (and only) element from the ‘data’ array and returns it as a new array, effectively removing the ‘data’ wrapper.

Always run a full workflow test to ensure the output matches your expectations. JSON manipulation can be tricky, but these methods should point you in the right direction.

I’ve encountered a similar issue in my N8N workflows before. Here’s what worked for me: use the ‘Set’ node after your current output. In the ‘Set’ node, create a new item with a value of ‘={{$json.data}}’. This effectively extracts the array from the ‘data’ key and sets it as the new output.

If that doesn’t do the trick, you could also try the ‘Function’ node. Write a simple JavaScript function like ‘return items[0].json.data;’. This should return just the array content.

Remember to test your workflow after each change. Sometimes, the preview doesn’t accurately reflect the final output, so it’s worth running the entire workflow to verify the result. Hope this helps solve your issue!