How can I replace a list of items in an array w/subset of list

Describe the problem/error/question

I’m trying to understand how JSON structures are handled in n8n and have not been able to solve the following:

I would like to replace an existing list inside an array with a subset of that list.
For example: Let’s say I have the following JSON structure:
[
{
“field1”: value,
“field2”: value,
“array”: [
{
“arrayField1”: value,
“arrayField2”: value
},
{
“arrayField1”: value,
“arrayField2”: value
}
],
“field3”: value,
“field4”: value
}
]

I would like to replace the list of items in “array” with a subset list:
Each item in the array is currently:
{
“arrayField1”: value,
“arrayField2”: value
}

But I want this:
{
“arrayField2”: value
}

The revised JSON now would look like this:
[
{
“field1”: value,
“field2”: value,
“array”: [
{
“array1Field2”: value
},
{
“array1Field2”: value
}
],
“field3”: 3,
“field4”: 4
}
]
I’ve been able to do this using an Edit Set node in JSON mode but that’s not transparent, I’d like to understand how it’s done.
I’m hoping there’s a combination of non-code nodes that can accomplish the same thing.

How can I do this? :thinking:

Thanks for your attention.

What is the error message (if any)?

No error messages

Please share your workflow

{
“nodes”: [
{
“parameters”: {},
“type”: “n8n-nodes-base.manualTrigger”,
“typeVersion”: 1,
“position”: [
-1380,
-620
],
“id”: “7ac6d5c3-7770-420d-8389-3914213d194c”,
“name”: “When clicking ‘Test workflow’”
},
{
“parameters”: {
“mode”: “raw”,
“jsonOutput”: “{\n"field1": 1,\n"field2": 2,\n"array":\n[\n {\n "arrayField1": 1,\n "arrayField2": 2\n },\n {\n "arrayField1": 3,\n "arrayField2": 4\n }\n],\n"field3": 3,\n"field4": 4\n}”,
“options”: {}
},
“type”: “n8n-nodes-base.set”,
“typeVersion”: 3.4,
“position”: [
-1160,
-620
],
“id”: “a7ce0231-d018-4708-99ea-c3ffe17b2358”,
“name”: “Sample Data”
},
{
“parameters”: {
“mode”: “raw”,
“jsonOutput”: “\n {\n "field1": 1,\n "field2": 2,\n "array": [\n {\n "arrayField2": 2\n },\n {\n "arrayField2": 4\n }\n ],\n "field3": 3,\n "field4": 4\n }”,
“options”: {}
},
“type”: “n8n-nodes-base.set”,
“typeVersion”: 3.4,
“position”: [
-980,
-620
],
“id”: “43f38b07-5b8e-48d4-ac99-0b514d977714”,
“name”: “Subset List”
}
],
“connections”: {
“When clicking ‘Test workflow’”: {
“main”: [
[
{
“node”: “Sample Data”,
“type”: “main”,
“index”: 0
}
]
]
},
“Sample Data”: {
“main”: [
[
{
“node”: “Subset List”,
“type”: “main”,
“index”: 0
}
]
]
}
},
“pinData”: {
“Sample Data”: [
{
“field1”: 1,
“field2”: 2,
“array”: [
{
“arrayField1”: 1,
“arrayField2”: 2
},
{
“arrayField1”: 3,
“arrayField2”: 4
}
],
“field3”: 3,
“field4”: 4
}
]
},
“meta”: {
“instanceId”: “bf55062cf7ddcdbf8e25875dbbbcd6b2d37792514cd66a0c31333838808e382c”
}
}

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

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

the general rule and the data structure is an array of objects, and every item has its own “json” key that contains all the item data

so in your case, you can achieve that by manipulating the json using the “Code” node or “Split out” with “Set” depending on what you really need

Reference

Yes, I am able to use Split Out and Edit Set to create a subset of the original list but how do I replace the original list with the revised list?

You have already got the transformation done.
Now you need a parallel branch that would exclude the transformed field from original input, and then merge the transformed data back into it.

Try the workflow below. The Code node is used only to generate the test data. Note how Set node is configured to remove fields.

If this suggestion resolves your issue please mark this post as a Solution.

1 Like

Thank you so much! I’ll close out my question.
:hugs:

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