Separate words within [] and define them as variable

I will receive a webhook with these variables json:
{
“first_name”: “leonardo”,
“phone_number”: “(47) 98828-9126”,
“order_status”: “abandoned”,
“cart_total”: “180.97”
}

I’ll get a custom MySQL text

I’ll get a text from the database mysql like this:

Hi [first_name],
your total [cart_total],
your status [order_status],
phone number [phone_number]

I want to separate the words and declare expression in them to be variable;

to get the information from the webhook and put it in the text that was taken from mysql

Hey @LEONARDO_SANTOS!

Welcome to the community. :slightly_smiling_face:

From your question I understood that you’re getting the the text from MySQL and you want to replace the variables like [first_name], [cart_total] etc. with the values you get from the Webhook. I created a similar workflow that might help. Here instead of the MySQL node, I am getting the text from a Set node. I have used the Function node to replace the variables with the values I get from the Webhook node. I have only two variables in my message and I am replacing them with the values (coming from the Webhook node) using the .replace() method. I hope this helps

{
  "nodes": [
    {
      "parameters": {
        "path": "2340d36b-e045-4e36-aae1-a04e776a9b55",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "webhookId": "2340d36b-e045-4e36-aae1-a04e776a9b55"
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "message_data",
              "value": "Hello [name]. The cost is [cost]"
            }
          ]
        },
        "options": {}
      },
      "name": "Set Message",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const name = items[0].json.query.name;\nconst cost = items[0].json.query.cost;\nlet message = items[0].json.message_data;\nmessage = message.replace(/\\[name\\]/,name)\nmessage = message.replace(/\\[cost\\]/, cost);\nreturn [{json:{message}}];"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Set Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Message": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thanks for the feedback.

Problem I have to get this text from a database:

I get a webhook with data:
{
“name”: “Ludmila Diniz Guerreiro”,
“cost”: “180.97”
}

fetch message data in MySQL

Olá [name], total de [cost],

I replace the [name] and [cust] data for json data

follow my project

{ "name": "suporte", "nodes": [ { "parameters": {}, "name": "Start", "type": "n8n-nodes-base.start", "typeVersion": 1, "position": [ 250, 300 ] }, { "parameters": { "httpMethod": "POST", "path": "suporte", "options": {} }, "name": "Webhook", "type": "n8n-nodes-base.webhook", "typeVersion": 1, "position": [ 170, 460 ], "webhookId": "2340d36b-e045-4e36-aae1-a04e776a9b55" }, { "parameters": { "values": { "string": [ { "name": "pegabanco", "value": "={{$json[\"texto\"]}}" } ] }, "options": {} }, "name": "Set Message", "type": "n8n-nodes-base.set", "typeVersion": 1, "position": [ 700, 420 ] }, { "parameters": { "functionCode": "const name = items[0].json.name;\nconst cost = items[0].json.cost;\nlet message = items[0].json.message_data;\nmessage = message.replace(/\\[name\\]/,name)\nmessage = message.replace(/\\[cost\\]/, cost);\nreturn [{json:{message}}];" }, "name": "Function", "type": "n8n-nodes-base.function", "typeVersion": 1, "position": [ 1000, 410 ] }, { "parameters": { "operation": "executeQuery", "query": "SELECT * FROM mensagem LIMIT 50 " }, "name": "MySQL", "type": "n8n-nodes-base.mySql", "typeVersion": 1, "position": [ 450, 560 ], "credentials": { "mySql": "cloudways" } } ], "connections": { "Webhook": { "main": [ [ { "node": "MySQL", "type": "main", "index": 0 } ] ] }, "Set Message": { "main": [ [ { "node": "Function", "type": "main", "index": 0 } ] ] }, "MySQL": { "main": [ [ { "node": "Set Message", "type": "main", "index": 0 } ] ] } }, "active": false, "settings": {}, "id": "7" }

Hey @LEONARDO_SANTOS!

You have to replace the Set node with the MySQL node. I have modified the workflow. Please take a look, and let me know if you still have any questions.

You will have to update the query in the MySQL node. Replace test with your table name - mensagem.

{
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "suporte",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        763,
        622
      ],
      "webhookId": "2340d36b-e045-4e36-aae1-a04e776a9b55"
    },
    {
      "parameters": {
        "functionCode": "const name = $item(0).$node[\"Webhook\"].json[\"body\"][\"name\"];\nconst cost = $item(0).$node[\"Webhook\"].json[\"body\"][\"cost\"];\nlet message = $item(0).$node[\"MySQL\"].json[\"message\"];\nmessage = message.replace(/\\[name\\]/,name)\nmessage = message.replace(/\\[cost\\]/, cost);\nreturn [{json:{message}}];"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1430,
        570
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT * FROM test LIMIT 1"
      },
      "name": "MySQL",
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 1,
      "position": [
        1060,
        600
      ],
      "credentials": {
        "mySql": "mysql-support"
      }
    }
  ],
  "connections": {
    "Webhook1": {
      "main": [
        [
          {
            "node": "MySQL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MySQL": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thanks, it worked.

how to save the message to not consult mysql every time there is a webhook called

making a cron every 15 minutes and getting the mysql message

I am glad that it worked!

In the workflow you shared above, you don’t want to query MySQL everytime a webhook request is made, rather query it every 15 minutes. Is this what you are trying to achieve? Let me know if I understood it correctly.

Also, I would suggest not to use 2 trigger nodes in a single workflow. Here is my explanation for that: Your workflow will trigger in every 15 mins due to the Cron node. Now, that you don’t have the name and cost of the user (which you were getting from the Webhook node), your workflow will terminate unsuccessfully.