Move Binary Data to JSON - Equivalent using the Code Node (iterate over Binary files)

Describe the problem/error/question

I’m using an Airtable list node to return records and will download attachments if they are present. I have a functioning workflow which send this data to a ‘Move Binary Data’ node and converts the file into a base64 string to be used later. My problem is that if more than 1 file is present, I need to be able to iterate over the binary files and convert each one - right now I am only able to access the first file (stored in ‘attachment_0’.

I have tried to write a JS function to do this and tried many iterations, but not managed to get it to work the way I need:

const items = [];
for (let i = 0; i < $input.item.json.fields.attachments.length; i++) {
  const binaryData = $input.item.binary[`attachments_${i}`].data;
  const base64Data = Buffer.from(binaryData).toString('base64');
    json: {
      attachment: base64Data

return items;

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey @Felix_is_stuck,

As a starting point the below takes an image from unsplash and converts the binary data to base64 using a code node, I suspect it should be that bad to adapt it to Airtable.

1 Like

Thank you @Jon! You got me on the right track, here is the finished code snippet which iterates over the attachments returned from the Airtable list node, converts to base64 string and assigns the string to a new data property within the attachment object.

// Loop over input items
for (const item of $input.all()) {
  // Get the attachments for the current item
  const attachments = item.json.fields.attachments;

  // Loop over each attachment
  for (let i = 0; i < attachments.length; i++) {
    // Get the binary data buffer for the current attachment
    let binaryDataBufferItem = await this.helpers.getBinaryDataBuffer(0, `attachments_${i}`);

    // Convert the binary data buffer to a base64 string
    const base64Data = Buffer.from(binaryDataBufferItem).toString('base64');

    // Add the base64 string to the current attachment object
    attachments[i].data = base64Data;

// Return the modified input items
return $input.all();

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