Loop through a JSON result set

I have a result set that comes back from an HTTP request and has the following JSON structure

[
   {
     "inte_data2": [
                             {...}, // 12 keys
                             {...}, // 13 keys
                             {...}, // 13 keys
                          ]
   }
]

Not really a node.js guy but I’m trying to do two things here … one loop through the results and pass them on to a lambda function on AWS. Everytime I use split in batches I get 1 big lump.

Also if I want to insert some values into the result set before I call the Lambda function.

Thanks.

Welcome to the community @Alykhan_Virani!

You can do that with a Function-Node and a slightly modified version of this.

Here the code:

return items[0].json.inte_data2.map(item => {
  return {
    json: {
      ...item,
      b: 'additional key'
    }
  }
});

And here an example workflow:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      inte_data2: [\n        {\n          a: 1\n        },\n        {\n          a: 2\n        }\n      ]\n    }\n  }\n];"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.inte_data2.map(item => {\n  return {\n    json: {\n      ...item,\n      b: 'additional key'\n    }\n  }\n});\n"
      },
      "name": "Split Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Split Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Thank you so much for the sample code… I implemented the function but I ended up getting an error.

ERROR: Cannot read property ‘map’ of undefined

Details

Stack

TypeError: Cannot read property 'map' of undefined
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:1:130
    at Object.<anonymous> (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:9:2)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1167:29)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:66:31)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:492:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:424:62
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Need help? Open Function documentation

Execute Workflow

Here is the JSON output…(I realize this is not an N8N issue but just my lack of understanding of Javascript. (I’m a Python guy :slight_smile: ).

[
    {
        "inte_data": [
            {
                "network": "twitter",
                "posted": "2021-05-28 04:41:37 +00000",
                "postid": "1398137358655045632",
                "text": "RT @BrianNoronha5: "The future of crypto mining is renewable": Argo Blockchain's CEO Peter Wall on 'green' bitcoin & sustainability #HODL #Bitcoin #BTC $BTC #ARB $ARBKF #ARBKF #ZCASH #ZEC $ZEC #cryptocurrency #Crypto https://t.co/5tXu663W9H via @bdaily",
                "lang": "en",
                "type": "link",
                "sentiment": "neutral",
                "url": "https://twitter.com/invbluesky_/status/1398137358655045632",
                "user": {
                    "userid": "1219286260772212736",
                    "name": "Blue Sky Capital",
                    "url": "https://twitter.com/invbluesky_",
                    "image": "https://pbs.twimg.com/profile_images/1289708519479017472/7XSGDBnF_normal.jpg",
                    "description": "Rule No.1: Never lose money, Rule No.2: Never forget rule No.1",
                    "location": "",
                    "influence": {
                        "name": "followers",
                        "count": 540
                    }
                },
                "urls": [
                    {
                        "short_url": "https://t.co/5tXu663W9H",
                        "url": "https://bdaily.co.uk/articles/2021/05/27/the-future-of-crypto-mining-is-renewable-argo-blockchains-ceo-peter-wall-on-green-bitcoin-and-sustainability",
                        "text": "bdaily.co.uk/articles/2021/…"
                    }
                ],
                "tags": [
                    {
                        "text": "HODL",
                        "url": "http://twitter.com/search/%23HODL"
                    },
                    {
                        "text": "Bitcoin",
                        "url": "http://twitter.com/search/%23Bitcoin"
                    },
                    {
                        "text": "BTC",
                        "url": "http://twitter.com/search/%23BTC"
                    },
                    {
                        "text": "ARB",
                        "url": "http://twitter.com/search/%23ARB"
                    },
                    {
                        "text": "ARBKF",
                        "url": "http://twitter.com/search/%23ARBKF"
                    },
                    {
                        "text": "ZCASH",
                        "url": "http://twitter.com/search/%23ZCASH"
                    },
                    {
                        "text": "ZEC",
                        "url": "http://twitter.com/search/%23ZEC"
                    },
                    {
                        "text": "cryptocurrency",
                        "url": "http://twitter.com/search/%23cryptocurrency"
                    },
                    {
                        "text": "Crypto",
                        "url": "http://twitter.com/search/%23Crypto"
                    }
                ],
                "user_mentions": [
                    {
                        "text": "Bdaily",
                        "url": "https://twitter.com/Bdaily"
                    },
                    {
                        "text": "BrianNoronha5",
                        "url": "https://twitter.com/BrianNoronha5"
                    }
                ]
            },
            {...
            }, // 12 keys
            {...
            }, // 12 keys
            {...
            }, // 12 keys
            {...
            }, // 12 keys
            {...
            }, // 13 keys
            {...
            }, // 12 keys
            {...
            }, // 12 keys
            {...
            }, // 13 keys
            {...
            }, // 12 keys
            {...
            }, // 12 keys
            {...
            }, // 12 keys
            {...
            }, // 12 keys
            {...
            }, // 12 keys
            {...
            }, // 12 keys
            {...
            }, // 12 keys
            {...
            }, // 13 keys
            {...
            }, // 12 keys
            {...
            }, // 12 keys
            {...
            } // 12 keys
        ]
    }
]

Sorry caught the typo… in my code. Its working perfectly. Thank you!

1 Like