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.

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”:

It allows moving data between binary and JSON.

1 Like

Please use 0.31.1 it contains some important changes.

resurecting instead of opening new.

I’m having a similar need here and though I’m on a much newer version and ‘move binary data’ is there, I’m not getting desired results

I have an API call that returns 8600 records. When I pull that in and run it through ‘move binary data’ and select all, I can see that it’s processing each entry and writing it to the file I specify. Overwriting. So I end up with 1 correct record in the file.

If I select ‘append’, then it doesn’t clear that file on the next run and malforms the output and grows it.

Really need 1 of 2 things to happen here.

  1. overrite file but append records within execution run
  2. truncate/delete file.

I can’t find a way to do either.

The ultimate goal here is to simply pull the API contents and cache them in a file. Unfortunately the vendor doesn’t let me filter this data out in the API call, so I pull the entire data set out on each execution. I want to just run this as a separate scheduled task and write that to a file, then operate on that cached file.

API JSON → json>binary → write binary file. This is where I’m either overwriting records and end up with 1, or I’m appending and it malforms it and grows it forever.

Ideas?

Hi @syadnom, this is the expected behaviour in n8n. Most n8n nodes will perform their action for every single incoming item.

If you’d like to have a single file with with the content of multiple items you’d first need to aggregate your data into a single item. For example like so:

This will leave you with a single file containing multiple items:

Hope this helps!

Going forward, perhaps you want to open a new thread instead of re-opening old ones? n8n has changed a lot in the past four years, so mixing old and new content often leads to confusion.

Thanks so much!

I don’t have that ‘aggregate’ node, v1.20.0 which is current docker from the n8nio repo…

found that aggregate was added in 1.22, updated and it’s there. This works.

@syadnom , in the previous version of n8n you would use Item Lists with “Summarize” option. For example,

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.