Hey everyone, I’m working on an N8N workflow and I’m stuck. I’ve got a JSON output that looks like this:
{
"data": [
{
"id": 21428
}
]
}
But what I really need is just the array inside, without the “data” key. So I want it to look like this:
[
{
"id": 21428
}
]
I’ve tried messing around with the item list and split output, but no luck so far. Any ideas on how to strip away that outer object and just keep the array? I’m not looking to get just one value, I need the whole array intact. Thanks in advance for any help!
I’ve encountered this issue before in my N8N workflows. The easiest way I found to extract the array from your JSON object is to use the ‘Set’ node. Here’s what you can do:
Add a ‘Set’ node after your current node.
In the ‘Set’ node, create a new item.
Set the ‘Name’ as something like ‘extractedArray’.
For the ‘Value’, use the following expression: {{$json.data}}
This expression directly accesses the ‘data’ array from your input JSON.
After this, your workflow will output just the array you need. I’ve used this method multiple times, and it’s always worked well for me. It’s simple and doesn’t require any complex manipulations.
Remember to test your workflow after making these changes to ensure it’s working as expected. Good luck with your project!
hey alex, i’ve dealt with this before. quick fix: use the ‘JSON’ node after your current one. in the ‘Keep Only’ field, just put ‘data’. it’ll strip everything else out and leave you with just the array. works like a charm for me every time. hope this helps!
In my experience with N8N, the Function node is your best bet for this task. It offers more flexibility than the Set node for complex data manipulation. Here’s a straightforward approach:
Add a Function node after your current node.
In the Function node, use this JavaScript code:
return {
json: $input.item.json.data
};
This code directly extracts the ‘data’ array and returns it as the new output.
I’ve used this method in several workflows where I needed to restructure JSON responses. It’s efficient and allows for more advanced operations if needed in the future. Just make sure to test the output to confirm it matches your requirements.