Get single JSON binary from the incoming data (not multiple ones)

Hi. I receive some JSON data and want to write it into a file. So first I use the Move JSON to Binary node and the apply Write Binary File.

The problem is that the “Move” node generates multiple binary items which are written then one by one to the file. So the file stores only the last record. And i need just one binary outcome which contains the whole array of data.

What should I do for that?

Before moving the binary data node, I would use a function node to merge all items into a single one. Check the example workflow below.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -500,
        200
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      uuid: 'v',\n      name: 'One',\n      uuid_parent: 'a',\n      name_parent: 'Blue',\n    }\n  },\n    {\n    json: {\n      uuid: 'w',\n      name: 'Two',\n      uuid_parent: 'a',\n      name_parent: 'Blue',\n    }\n  },\n      {\n    json: {\n      uuid: 'x',\n      name: 'Three',\n      uuid_parent: 'b',\n      name_parent: 'Green',\n    }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -250,
        200
      ],
      "notesInFlow": true,
      "notes": "Mockup data"
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: items.map((item) => item.json)\n  }\n]"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -30,
        200
      ],
      "notesInFlow": true,
      "notes": "merge items"
    },
    {
      "parameters": {
        "mode": "jsonToBinary",
        "options": {}
      },
      "name": "Move Binary Data",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        170,
        200
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function1": {
      "main": [
        [
          {
            "node": "Move Binary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

@RicardoE105 Thank you! What is the best way to decode it back to an array? It looks like that I have to take just the first item and get the array back. When I use a Function node like

return items[0];

I receve ERROR: Always an Array of items has to be returned!

After you write to the file system, you can use the Move Binary Data again, but using Binary to JSON, and finally use a function node to convert it into multiple items.

Function node code:

return items[0].json.map(item => ({ json: item }))
Example workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      uuid: 'v',\n      name: 'One',\n      uuid_parent: 'a',\n      name_parent: 'Blue',\n    }\n  },\n    {\n    json: {\n      uuid: 'w',\n      name: 'Two',\n      uuid_parent: 'a',\n      name_parent: 'Blue',\n    }\n  },\n      {\n    json: {\n      uuid: 'x',\n      name: 'Three',\n      uuid_parent: 'b',\n      name_parent: 'Green',\n    }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        500,
        300
      ],
      "notesInFlow": true,
      "notes": "Mockup data"
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: items.map((item) => item.json)\n  }\n]"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        700,
        300
      ],
      "notesInFlow": true,
      "notes": "merge items"
    },
    {
      "parameters": {
        "mode": "jsonToBinary",
        "options": {
          "keepSource": false
        }
      },
      "name": "Move Binary Data",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.map(item => ({ json: item }))"
      },
      "name": "Function2",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1560,
        300
      ],
      "notesInFlow": true,
      "notes": "Revert back"
    },
    {
      "parameters": {
        "fileName": "/Users/ricardoe105/.n8n/data.json"
      },
      "name": "Write Binary File",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        1150,
        300
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "name": "Move Binary Data1",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        1350,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function1": {
      "main": [
        [
          {
            "node": "Move Binary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Move Binary Data": {
      "main": [
        [
          {
            "node": "Write Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write Binary File": {
      "main": [
        [
          {
            "node": "Move Binary Data1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Move Binary Data1": {
      "main": [
        [
          {
            "node": "Function2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Ok, I’ll try it, thank you