Remove json characters when encoding for base64 format

I have a doubt,

when encoding a value test123 using the SET feature it gets standard json characters:

[
{
"data": "test123"
}
]

base64 result:
W3siZGF0YSI6InRlc3QxMjMifV0=

decode:
[{"data": "test123"}]

how do I remove everything and leave only the test123 value without quotes?

I need this to use an application that accepts only the value without any character like: [, {, ",: etc

code workflow:

{
  "nodes": [
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "data",
              "value": "=test123"
            }
          ]
        },
        "options": {
          "dotNotation": false
        }
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        650,
        250
      ]
    },
    {
      "parameters": {
        "functionCode": "const data = []\n\nfor (const item of items) {\n  data.push(item.json)\n}\n\nconst dataBase64 = Buffer.from(JSON.stringify(data)).toString('base64');\n\nreturn [\n  {\n    json: {\n      dataBase64\n    }\n  }\n]\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        840,
        250
      ],
      "notesInFlow": true,
      "notes": "Base64 encoding"
    }
  ],
  "connections": {
    "Set1": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Welcome to the community @danilorq!

Is that what you want?

{
  "nodes": [
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "data",
              "value": "=test123"
            }
          ]
        },
        "options": {
          "dotNotation": false
        }
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        450
      ]
    },
    {
      "parameters": {
        "functionCode": "const data = []\n\nfor (const item of items) {\n  data.push(item.json.data)\n}\n\nconst dataBase64 = Buffer.from(JSON.stringify(data)).toString('base64');\n\nreturn [\n  {\n    json: {\n      dataBase64\n    }\n  }\n]\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        640,
        450
      ],
      "notesInFlow": true,
      "notes": "Base64 encoding"
    }
  ],
  "connections": {
    "Set1": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

thanks for solving, i am a fan of n8n and beginner user.

it cannot contain any additional characters ( [ or " ), only pure data.

current output:
["test123"]

how it should be:
test123

thank you again!

Ah because of your example nodes did I think you actually wanted to have all of them combined.

In this case you can use a Function-Node with this code:

return items.map(item => {
  return {
    json: {
      dataBase64: Buffer.from(JSON.stringify(item.json.data)).toString('base64'),
    }
  }
});
1 Like

it almost worked, there’s only one problem.

is it possible to remove the quotation marks from the result?

leaving only the pure value.

currently:
"test123"

what it needs to be:
test123

I took a print of the result to exemplify:
https://prnt.sc/wume81

Ah yes. Because it is a string the stringify is not needed. So only:

return items.map(item => {
  return {
    json: {
      dataBase64: Buffer.from(item.json.data).toString('base64'),
    }
  }
});
1 Like

thank you so much!!! :clap: