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: