I’m working on an N8N workflow and need help with JSON manipulation. My current output has a ‘data’ key wrapping an array. I want to remove this key and just return the array directly. Here’s what I have now:
{
"data": [
{
"recordId": 9876
}
]
}
And this is what I’m trying to get:
[
{
"recordId": 9876
}
]
I’ve tried using the ‘Split In Batches’ node to break up the output, but it didn’t work. Any ideas on how to strip off that outer object and just keep the array? I’m not looking to extract a single value, but rather the whole array structure. Thanks for any suggestions!
hey there! i’ve run into this before. easy fix: use the ‘JSON Parse’ node. set the ‘Property Name’ to ‘data’ and boom, you’ll get just the array. it’s like magic for stripping off that pesky outer layer. lemme know if u need more help!
Having worked extensively with N8N workflows, I can confidently suggest using the ‘Function’ node for this task. It’s a versatile tool that allows you to write custom JavaScript code to manipulate your data. Here’s what you can do:
- Add a ‘Function’ node after your current output.
- In the ‘Function’ node, use this simple code:
return items.map(item => item.json.data[0]);
This code extracts the first (and only) element from each ‘data’ array and returns it directly. It’s efficient and doesn’t require additional nodes.
If you’re dealing with multiple items, this approach will work for each of them, maintaining the structure you desire. It’s a clean solution that I’ve successfully implemented in similar scenarios.
One approach is to use the Set node in N8N to modify the current output. Instead of dealing with a list in a separate batch, you can easily extract the array by creating a new property that points to the inner data of the JSON object using the expression {{$json.data}}. This effectively replaces your original output with just the array and removes the extra wrapper. With this method, you ensure that your workflow remains clean and the data structure is exactly what you want for subsequent operations in N8N.