Prettify the json to binary written

Describe the issue/error/question

I can save my json to a file and git add+commit+push it and it works fine… but…

how can I have a prettier json? in js, that is as simple as JSON.stringify(data,null,2), but I don’t see that option on the “json to binary” node.

Am I missing something?

What is the error message (if any)?

no error message, just a json without any space

Please share the workflow

I’m not sue I can share without leaking private data, please advice if needed :wink:

Hi @xavier, welcome to the community :tada:

You can actually use JSON.stringify in n8n if you like. The Function and Function Item nodes let you execute JavaScript code.

After using this method, you can then use the Use Raw Data option of the Move Binary Data node to prevent it from applying it’s own logic:
image

The resulting file will then have the prettified JSON you have in mind and can be further processed:
image

Here’s an example workflow creating such a file and writing it to the hard drive at the end:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "Foo",
              "value": "Bar"
            }
          ],
          "number": [
            {
              "name": "Baz",
              "value": 123
            }
          ]
        },
        "options": {}
      },
      "name": "Set Dummy Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "item.prettified = JSON.stringify(item, null, 2);\n\nreturn item;"
      },
      "name": "Prettify",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "mode": "jsonToBinary",
        "convertAllData": false,
        "sourceKey": "prettified",
        "options": {
          "useRawData": true
        }
      },
      "name": "Move Binary Data",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        880,
        300
      ]
    },
    {
      "parameters": {
        "fileName": "/home/node/Desktop/example.json"
      },
      "name": "Write Binary File",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        1080,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Dummy Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Dummy Data": {
      "main": [
        [
          {
            "node": "Prettify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prettify": {
      "main": [
        [
          {
            "node": "Move Binary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Move Binary Data": {
      "main": [
        [
          {
            "node": "Write Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like