JSON parsing problem

There is a task to receive JSON, to process and save in mySQL. The primitive JS function works fine in the browser, but doesn’t want to work in n8n. Can you suggest what the problem is and how to solve it?
JSON:

{
    "data": [
        {
            "id": "a1",
            "caption": "aa1",
            "url": [
                "a1.csv",
                "a1.json",
                "a1.xml"
            ],
            "format": "CSV, JSON, XML"
        },
        {
            "id": "a2",
            "caption": "aa2",
            "url": [
                "a2.json",
                "a2.xml",
                "a2.csv"
            ],
            "format": "CSV, JSON, XML"
        },
        {
            "id": "a3",
            "caption": "aa3",
            "url": [
                "a3.json",
                "a3.xml",
                "a3.csv"
            ],
            "format": "CSV, JSON, XML"
        },
        {
            "id": "a4",
            "caption": "aa4",
            "url": [
                "a4.csv",
                "a4.xml",
                "a4.json"
            ],
            "format": "CSV, JSON, XML"
        }
    ]
}

The code:

for (item of items) {
    if (item.url[0].substr(-4,4) == "json") {
        newUrl = item.url[0]
    } else if (item.url[1].substr(-4,4) == "json") {
        newUrl = item.url[1]
    } else {
        newUrl = item.url[2]
    }
    item.url = newUrl;
    delete item ['format']
}
return items

Workflows:
001

Hi @vkusit, welcome to the community!

Based on your description I understand you have a single item with a data field like so in n8n:

And you would then like to iterate through the individual objects inside of the data field. Your idea would work fine in n8n, but your code currently tries to iterate through n8n items instead rather than the objects inside of the data field

If you iterate through the data objects of your first item instead and return a valid n8n data structure, you’re probably good to go. I’ve build a quick example workflow which you can copy paste right into your n8n canvas showing the idea:

Example Workflow

Hope this helps! Give me a shout if you have any queries on this :slight_smile:

Thanks for the help, but it doesn’t work.
Your example works, in practice it’s an error.
ERROR: items[0].json.data is not iterable [Line 1]
I don’t even know what to think…

Hey @vkusit, I tried this on my end and the example seems to be working as expected with the data obtained from your URL:

Example Workflow

Are you perhaps running the wrong branch of your workflow? Could you try using the updated example above? This should have only the HTTP Request node fetching your data and a Function node transforming it.