How to iterate/loop a JSON of objects?

Describe the problem/error/question

Below I have pasted an HTTP node that gets a JSON from a website. What I want to do is process it like a list with a Loop node, iterating each object in the list. The list contains objects, which are the names of softwares I am tracking, and each object contains multiple versions, and a date field called “updated”. What I want to do is go through each one, get the name of it and the updated date, and then after that I will run those through a Compare node to another set of data. I can take it from there. But the trouble I am having is converting this JSON set of objects into an iterate-able list that a Loop node will process properly.

Please share your workflow

Information on your n8n setup

  • n8n version: 1.110.1
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Local/Docker

This is what the output of the above workflow looks like currently:

Just a single line.

And I want it to instead be something like this:

Split into a list of all the objects.

However I cannot use a Split Out node, because the output is flat.

I need something like this:

But not as a single output item, instead as a list of objects, INCLUDING the name of the object itself.

So that I can add a Loop node after that and iterate each one, to extract the software Name and the Updated date from it.

How can I do this?

hello @protechtedd, welcome to :n8n: community!

First, in the HTTP node, go to Options > Response and set the Response Format to JSON:

This will output the response as formatted JSON:

Next, use a Set node to assign the entire object to a field. You can then manipulate it as needed:

Ehy @protechtedd
In n8n you don’t actually need a Loop node to process a list if your data is already an array, because nodes iterate items automatically.
The issue is that it comes back as an object of objects, not an array. To make it iterable you first need to transform it into a list of items. You can do this with the Item Lists node using Split Out Items, or more flexibly with a Code node that maps the object into an array of items containing fields like name and updated.

Once the data is an array, n8n will process each plugin as a separate item, and if you want to control the flow item by item you can then add a Loop Over Items (Split in Batches) node with batch size set to 1 before passing them into the Compare node. This approach ensures each software entry is handled individually with the fields you need.

1 Like

Thank you both for the reply. First I have set the HTTP node to Response: JSON.

@Gallo_AIA I am still learning how to work with objects vs arrays within n8n. I think conceptually what you said makes sense, however I am trying to use the Split Out node as you suggested but I’m still a bit lost of what node to put between the HTTP node and the Split Out node, my goal being to iterate through them.

What would a Code node look like for mapping objects into an array which includes the name and updated fields? If I had the end result to look at, or a similar example, I could learn it very fast.

1 Like

I added a code node right after the HTTP Request,
Then i can directly loop through from the return value of the code node.
I think there are some type issues in the code, needs to be optimized.

const outputItems = ;

for (const item of $input.all()) {
try {
const parsedData = JSON.parse(item.json.data);

for (const [pluginName, pluginData] of Object.entries(parsedData)) {
const pluginObject = {
name: pluginName,
totalDownloads: pluginData.downloads || 0,
lastUpdated: pluginData.updated || null,
versions: {}
};

 for (const [key, value] of Object.entries(pluginData)) {
   if (key !== 'downloads' && key !== 'updated') {
     pluginObject.versions[key] = value;
   }
 }
 
 outputItems.push({ json: pluginObject });

}

} catch (error) {
outputItems.push({
json: {
error: Failed to parse JSON: ${error.message},
originalData: item.json.data
}
});
}
}

return outputItems;

You can try this one:

@raybankless This looks very close to what I want! Would you mind copy pasting the Code node itself from your workflow in a reply? I tried to copy paste the code in your message into a Code node in my own workflow, but it gives me an error and I’m not sure why.

I think the code you used is a bit wrong. I am also writing these with Claude, so the code part is a bit on you i guess :slight_smile:

const outputItems = ;

for (const item of $input.all()) {
try {
const parsedData = JSON.parse(item.json.data);

for (const [pluginName, pluginData] of Object.entries(parsedData)) {
const pluginObject = {
name: pluginName,
totalDownloads: pluginData.downloads || 0,
lastUpdated: pluginData.updated || null,
versions: {}
};

 for (const [key, value] of Object.entries(pluginData)) {
   if (key !== 'downloads' && key !== 'updated') {
     pluginObject.versions[key] = value;
   }
 }
 
 outputItems.push({ json: pluginObject });

}

} catch (error) {
outputItems.push({
json: {
error: Failed to parse JSON: ${error.message},
originalData: item.json.data
}
});
}
}

return outputItems;

@raybankless I think there is still something wrong with the formatting, it wont paste into my workflow. Try wrapping it in a codeblock in the message editor using 3 ticks on top and bottom ```

@mohamed3nan Your solution looks good too, however I would need one more step added to your Code node, which I don’t know how to do; I need the Name to be included in the iteration. Looking at raybankless’s screenshot above, I need it to look like this, so I can pull the Name and the LastUpdated dates out as I iterate through the list.

I don’t know if i’m copying wrong but this is how it works for me
———————

{

"nodes": \[

  {

“parameters”: {

“jsCode”: “const outputItems = ;\n\nfor (const item of $input.all()) {\n try {\n const parsedData = JSON.parse(item.json.data);\n \n for (const [pluginName, pluginData] of Object.entries(parsedData)) {\n const pluginObject = {\n name: pluginName,\n totalDownloads: pluginData.downloads || 0,\n lastUpdated: pluginData.updated || null,\n versions: {}\n };\n \n for (const [key, value] of Object.entries(pluginData)) {\n if (key !== ‘downloads’ && key !== ‘updated’) {\n pluginObject.versions[key] = value;\n }\n }\n \n outputItems.push({ json: pluginObject });\n }\n \n } catch (error) {\n outputItems.push({\n json: {\n error: `Failed to parse JSON: ${error.message}`,\n originalData: item.json.data\n }\n });\n }\n}\n\nreturn outputItems;”

    },

“type”: “n8n-nodes-base.code”,

“typeVersion”: 2,

“position”: [

560,

48

    \],

“id”: “c272686f-18d9-4b47-9a9f-2601618c633a”,

“name”: “Code”

  }

\],

“connections”: {

“Code”: {

“main”: [

      \[\]

    \]

  }

},

“pinData”: {},

“meta”: {

“instanceId”: “727ad39836ca47a4a1b152e9a5fe1af7095e985063493ccd21ceb3dff6f01cf7”

}

}

I couldn’t upload JSON here so uploaded the whole workflow to drive

Hope this helps

1 Like

@raybankless Yes that google drive link worked, thank you for doing that! Strange that the editor here wouldn’t let you do it. This is exactly what I needed. Thanks again, this helps heaps

You said you asked Claude to help with the creation of the Code node? I may have to check that out, I haven’t used Claude yet but keep hearing about it. What was your prompt?

1 Like

at this point it’s a javascript code and data manipulation, here is it btw:

Thank you! I’ll learn something from this too in conjunction with the other replies.

No worries!
Kindly, if my replies were the reason for the solution, kindly mark it as the accepted answer..

So happy that it helped you. About the code, i shared a piece of your raw response data and then prompted this:

————————————
I am using n8n. There i have an http request returns like i shared. It returns the data about the software i follow. name, download, versions, updated … not sure what exactly in there but you can read from the example part i sent.

I need to turn this to a proper json. now it is just data object and some text in it. i need a proper json with all fields are sub objects in the greater json. i can use a code node and programmatically do this. but i don’t know how to code it

an example of how code node is used:
// Loop over input items and add a new field called ‘myNewField’ to the JSON of each one for (const item of $input.all()) { item.json.myNewField = 1; } return $input.all();

this way i can use the loop node after the code node and iterate through what i want
———————————–

then i had to refine it a little to remove the plugins wrapper that hold the individual software data.

1 Like

Very helpful, I’ll take a look at Claude and see if it can help me going forward!

Also I think I know why your pasted code earlier wasn’t working. In the reply editor here on the website, there is a button at the top (if you expand fullscreen the editor) called “Preformatted text”, if you copy a whole node in n8n and paste it into this, the reply will include it as an actual node, and other viewers can interact with it from the website directly, or copy paste it into their own n8n. Just for future reference, hope it helps.

1 Like

found it, thank you very much. i was trying with the Blockquote