How do I save the output of a Notion node as a JSON file?

I am trying to save the output of a Notion > Database Page > Get Many node as a JSON file on my local computer (running community n8n). But I am having trouble with the JSON formatting to make it work with future steps.

Screenshots below:

  • Output from Notion Get Many node:
  • Output from JSON to Binary node:
  • The Write Binary File output looks the same as above (when I use Append, which seems required otherwise it only writes the last record):
  • Then immediately read that file.
  • Output from Read Binary File: node:
    image
  • Output from Binary to JSON node:
  • You can see above that the last output is in a different format. It’s like the entire JSON got stored inside a dictionary or object or something (still learning JSON structures).

What can I do to write and then read that Notion output JSON data in the exact same format as I first got it (as a table)?

Hi @protechtedd :wave: Deleted my other reply, I see what you mean now :sweat_smile:

Can you try a code node after your Binary to Json node, with this:
return JSON.parse( '[' + $input.first().json.data.replaceAll('}{', '},{') + ']' )

That should split out the JSON for you in the format you expect :+1:

@EmeraldHerald That did work, however is it possible to change this code to go somewhere before the “Write JSON” node?

The problem with doing it during the “read file” phase is that the original file is still being written as an invalid JSON. This causes external apps to be unable to use the file.

Additionally, the node “Write JSON” is iterating and overwriting the file as individual records, instead of a single file with all records as an array. If I activate the node’s option “Append”, it does add them all, but the output JSON is no longer valid. Perhaps this can be fixed with code?

Update: I found this code snippet to try injecting before converting the JSON to binary then writing to file. It seems to format the JSON as valid, but the code still needs modifying because it wraps everything inside “data” and changes the formatting compared to the original (from Notion). Could we use this at all, or should I discard it for another solution?

return [{
  json: {
    data: items.map(e => e.json)
  }
}];

Okay, I spent some time over the weekend scouring for solutions. I solved one half of the problem, but the other half still isn’t working. I am open to changing anything / everything about this if you think it needs reworking. Am I fundamentally thinking about this correctly?

A quick recap of my goal: To write the output JSON from a Notion node into a valid JSON file, then be able to read that same file into a JSON/Table that n8n can compare via Comparison node with both Notion output and JSON file. Essentially, comparing a previous run to the current run to detect changes.

I updated the workflow from my original example. The “Write to JSON” row successfully converts and writes the Notion data to a JSON file. The “Read from JSON” row reads the saved file, but stores the output as a different format than expected, especially when compared to the original Notion format.

The strange thing to me is that when I copy the text from the Output > JSON tab from each, and paste them into a “text difference” tool, absolutely nothing is different between them. So this tells me that n8n is just parsing the data differently than I’m expecting for some reason; Do I need some node after “Binary to JSON” before sending to the Comparison node?

Workflow:

Screenshots below from the output of the final Comparison node.

Input A: Original Notion data

Input B: Read JSON file data

This was mainly thanks to @Jon so credit where credit is due - but can you try the following:

  • Change Binary to JSON to not set all data
  • Add JSON Parse Option
  • Add an Item Lists Node > Split out Items

That should fix this up for you!

1 Like

Just following up here that I have been using a system that works well for my needs. In case anyone comes here looking for help with this, here is an example set of nodes. This primarily works for Notion nodes, but could help with other types too. The ? nodes are a custom addon called “@n-octo-n/n8n-nodes-json-database”

2 Likes

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