How to read Binary data

I trying to read binary data and send it to other nodes. In this example, I’ll send the data as a message to Telegram.

Basic setup:

Read Binary File > Telegram

In the telegram, I try to send a message using the data read from the file as part of it., but I can’t do that. When I select to read the raw data in the path(assuming data was saved in the data key):
Nodes > Read Binary Files > Output Data > Binary > data > [More button] Raw value, just like the picture above:

image

But when I select it, It doesn’t show the binary file, but some JS object representation:

{{$node["Read Binary File"].binary.data}}
that evaluates to
{"mimeType":"text/plain","fileName":"TODO"} instead of the contents.

image

I tried other options but I couldn’t get the data.

The example flow in JSON
{
  "name": "pp testing",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "filePath": "../TODO"
      },
      "name": "Read Binary File",
      "type": "n8n-nodes-base.readBinaryFile",
      "typeVersion": 1,
      "position": [
        400,
        300
      ]
    },
    {
      "parameters": {
        "chatId": "",
        "text": "={{$node[\"Read Binary File\"].binary.data}}",
        "additionalFields": {}
      },
      "name": "Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        550,
        300
      ],
      "credentials": {
        "telegramApi": "Daemoniac Bot"
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Read Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Binary File": {
      "main": [
        [
          {
            "node": "Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "8"
}

You first have to use the Move Binary Data Node to move the data from binary to json. As soon as you did that, is it available in the Expression-Editor.

You can find the documentation here:

I looked at that, but It just seems to able to handle JSON format, not text formats in general. What if the binary data isn’t JSON?

The content of the binary data is written to the JSON-Structure in a variable. Then you can pass and use this variable in your telegram node.

Well, I can’t find this here. Here is the screenshot of the possible thinks to select:

image

Here an example workflow:

I see, using the move from binary to create a JSON that contains the data in a key.

But this seems odd to me. Maybe it has as technical reason for that, but wouldn’t be better if instead of using an extra node for that, this kind of data would be available directly?

For instance, if the binary > data could have a field with the acutal data, instead of using a extra node for that? This would be way better and would make more sense. I can even volunteer to implement this as a PR.

2 Likes

The data is available directly, as binary data.

Sure, it would be easier in your single specific use case but as the node says does it read binary data which would normally not make sense loading it directly into the json data. For that reason does it get loaded to the binary data.
So it makes sense for a few use-cases but not for most other ones. Lets say makes sense for text-files, does not make any sense for xls, png, pdf, jpg, mov, mp3, … does it make sense for csv, not so sure, really depends.
And even for the text-file, does it really depend what exactly you are doing with the data afterward. If you need the content directly, you want it in json, but if you want to upload it directly to Dropbox or send it as a file via HTTP, not so much.

Follow up on the topic.

I am trying to get data from the webscraper.io API. I get a file with json inside.
How can I turn the binary file containing json into json ? And get each row one by one to fill in a database ?
I tried a Move Binary data node but it doesn’t realy turn the binary into json as I thought it would (my missunderstanding)

API documentation :

My workflow :

{
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "getdata",
        "options": {}
      },
      "name": "Webhook1",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "url": "=https://api.webscraper.io/api/v1/scraping-job/{{$json[\"body\"][\"scrapingjob_id\"]}}/json?api_token=xxxxx",
        "responseFormat": "file",
        "dataPropertyName": "data_scrap",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "setAllData": false,
        "sourceKey": "data_scrap",
        "destinationKey": "data_json",
        "options": {}
      },
      "name": "Move Binary Data",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        820,
        300
      ]
    }
  ],
  "connections": {
    "Webhook1": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Move Binary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thanks

PS : I tried using the “set all data” function but I get an error

Maybe the problem comes from the file format ?

If the file would be JSON it would work but in this case, does it look like it is not. At least the screenshot you posted above is not valid JSON. Do they have different output formats and maybe one of them returning actual valid JSON?

You are right and I though the same about the Json.

Therefore I moved to CSV format and used a Spreadsheet File node which is working fine at the very moment

Sounds good. Would also be possible to make it work with their strange one JSON object per line format but would require some custom code in a Function-Node. Wonder why they do not simply return an array like every other REST API.