Write JSON to file

How do I write JSON data to file? I tried using the “write binary file” node, but can’t get that to work with JSON, it only seems to work with the xls file.

Thanks again so much!

Uh yes, you are right! The problem is that the “Write Binary File” expects binary data. The JSON data is, however, JSON :wink: There should really be a node that allows moving data around between both of them.

For now, it can be done with a Function-Node. At least till a proper one is in place. Pasted an example of how you can do that to the end of the message. The first node you can remove, it is only to create the example data. The second node named “Make Binary” is the important one with the custom code which makes the data binary and writes it to the correct location.

{
  "nodes": [
    {
      "parameters": {
        "fileName": "test.json"
      },
      "name": "Write Binary File",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        800,
        350
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].binary = {\n  data: {\n    data: new Buffer(JSON.stringify(items[0].json, null, 2)).toString('base64')\n  }\n};\nreturn items;"
      },
      "name": "Make Binary",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        350
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].json = {\n  \"text\": \"asdf\",\n  \"number\": 1\n};\nreturn items;"
      },
      "name": "Create Example Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        390,
        350
      ]
    }
  ],
  "connections": {
    "Make Binary": {
      "main": [
        [
          {
            "node": "Write Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Example Data": {
      "main": [
        [
          {
            "node": "Make Binary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Uh sorry always forget that people maybe do not know what they are supposed to do with the JSON I post.

You can simply copy the JSON above, click once in the n8n window and then paste (CTRL + v). It will then create the nodes.

Thanks again Jan! U da man!

I posted a workflow with your example:

EXAMPLE: Write JSON to Disk (Binary)

1 Like

Great, thanks a lot!

In the latest version of [email protected] there is now a special node called “Move Binary Data”:
https://n8n.io/nodes/n8n-nodes-base.moveBinaryData
It allows moving data between binary and JSON.

Please use 0.31.1 it contains some important changes.