Correct way of creating spreadsheet data from json?

Hi all.
I’m facing an issue with converting json to spreadsheet data (any format).

[n8n v0.118.1]

Here's an example workflow:
{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const result = [];\nfor(let i=0; i<10; i++){\n  result.push({\n    a: i,\n    b: i*2,\n    c: String(i)\n  });\n}\nreturn [{json:result}];"
      },
      "name": "Function5",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1160,
        -300
      ]
    },
    {
      "parameters": {
        "operation": "toFile",
        "fileFormat": "csv",
        "options": {}
      },
      "name": "Spreadsheet File2",
      "type": "n8n-nodes-base.spreadsheetFile",
      "typeVersion": 1,
      "position": [
        1330,
        -300
      ]
    },
    {
      "parameters": {
        "fileName": "/data/temp.csv"
      },
      "name": "Write Binary File2",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        1490,
        -300
      ]
    }
  ],
  "connections": {
    "Function5": {
      "main": [
        [
          {
            "node": "Spreadsheet File2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Spreadsheet File2": {
      "main": [
        [
          {
            "node": "Write Binary File2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

The Actual output:

0.a 0.b 0.c 1.a 1.b 1.c 2.a 2.b 2.c 3.a 3.b 3.c 4.a 4.b 4.c 5.a 5.b 5.c 6.a 6.b 6.c 7.a 7.b 7.c 8.a 8.b 8.c 9.a 9.b 9.c
0 0 0 1 2 1 2 4 2 3 6 3 4 8 4 5 10 5 6 12 6 7 14 7 8 16 8 9 18 9

The Expected output:

a b c
0 0 0
1 2 1
2 4 2
3 6 3
4 8 4
5 10 5
6 12 6
7 14 7
8 16 8
9 18 9

Would like to know if i’m:

  • expecting the wrong output/there’s something wrong with my workflow, or
  • there’s a bug with the node
    ?

Hi @shrey-42 , your logic is correct, only a small detail missing;
your Function node return one single item which contain all the lines, while you need to return multiple items (where each item contain one line)

I added a small modification to your code in the Function node and that should solve it, please give a try and let me know if that helps;

Also, i suggest checking this livestream, where Ivan and Tanay explained in great details how to deal with the data in n8n.
Function node code;

const result = [];
for(let i=0; i<10; i++){
  result.push({
    json:{
      a: i,
      b: i*2,
      c: String(i)
    }
  });
}
return result;

workflow json

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const result = [];\nfor(let i=0; i<10; i++){\n  result.push({\n    json:{\n      a: i,\n      b: i*2,\n      c: String(i)\n    }\n  });\n}\nreturn result;"
      },
      "name": "Function5",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        550,
        310
      ]
    },
    {
      "parameters": {
        "operation": "toFile",
        "fileFormat": "csv",
        "options": {}
      },
      "name": "Spreadsheet File2",
      "type": "n8n-nodes-base.spreadsheetFile",
      "typeVersion": 1,
      "position": [
        710,
        310
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "name": "Spreadsheet File",
      "type": "n8n-nodes-base.spreadsheetFile",
      "typeVersion": 1,
      "position": [
        880,
        310
      ]
    }
  ],
  "connections": {
    "Function5": {
      "main": [
        [
          {
            "node": "Spreadsheet File2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Spreadsheet File2": {
      "main": [
        [
          {
            "node": "Spreadsheet File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
2 Likes

Hi @dali , thanks for your response.

Didn’t realise that the table rows needed to be sent as individual items.