Set query parameter to default if not set by URL

HI

want to get from a mysql DB the list of entries when the ID is not part of the URL. IT works, but I think I make it too complicated …

https://n8n_web.domain.test/webhook/SQMS/TOPIC/READ
https://n8n_web.domain.test/webhook/SQMS/TOPIC/READ?topic_id=12

webhook → if topic_id is empty
→ True - Mysql node SELECT * FROM sqms_topic;
→ False - Mysql node SELECT * FROM sqms_topic where sqms_topic_id = {{$json[“query”][“topic_id”]}};

Then I make two identical calls (for each path) to a translation API

There must be a smarter way … can someone help me?

  1. Do I really need 2 mysql and 2 http query nodes or can I send the query dynamically depending on topic_id is sent or not?
  2. I tried to add a merge node after the 2 sql calls but I could only add 1 http request but I failed …
  3. the http request node returns only the last result - how can I merge the sql query result and the translation result?

Thanks - this is my first more complex workflow-
rob

{
  "name": "SQMS_ICO_TOPIC_READ",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -900,
        130
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "=SELECT * FROM `sqms_topic`;"
      },
      "name": "SQMS_TOPIC_READ",
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 1,
      "position": [
        -380,
        250
      ],
      "credentials": {
        "mySql": "ICO SQMS MARIADB"
      }
    },
    {
      "parameters": {
        "path": "SQMS/TOPIC/READ",
        "responseMode": "lastNode",
        "responseData": "allEntries",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              }
            ]
          }
        }
      },
      "name": "TOPIC_READ",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        -880,
        370
      ],
      "webhookId": "f2a81bd0-7170-444b-b686-1be165a6b9ad",
      "color": "#668855"
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "https://ico_edums_translate_api.domain.test/v1/translation",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "text",
              "value": "={{$json[\"name\"]}}"
            },
            {
              "name": "target_lang",
              "value": "es"
            },
            {
              "name": "cache",
              "value": "true"
            },
            {
              "name": "source_lang",
              "value": "en"
            }
          ]
        }
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        -120,
        250
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "=SELECT * FROM `sqms_topic` where `sqms_topic_id` = {{$json[\"query\"][\"topic_id\"]}};"
      },
      "name": "SQMS_TOPIC_READID",
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 1,
      "position": [
        -380,
        450
      ],
      "credentials": {
        "mySql": "ICO SQMS MARIADB"
      }
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "https://ico_edums_translate_api.domain.test/v1/translation",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "text",
              "value": "={{$json[\"name\"]}}"
            },
            {
              "name": "target_lang",
              "value": "es"
            },
            {
              "name": "cache",
              "value": "true"
            },
            {
              "name": "source_lang",
              "value": "en"
            }
          ]
        }
      },
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        -120,
        450
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"query\"][\"topic_id\"]}}",
              "operation": "isEmpty"
            }
          ]
        }
      },
      "name": "IF_TOPICID_EMPTY",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        -680,
        370
      ]
    }
  ],
  "connections": {
    "TOPIC_READ": {
      "main": [
        [
          {
            "node": "IF_TOPICID_EMPTY",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SQMS_TOPIC_READ": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SQMS_TOPIC_READID": {
      "main": [
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF_TOPICID_EMPTY": {
      "main": [
        [
          {
            "node": "SQMS_TOPIC_READ",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "SQMS_TOPIC_READID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {},
  "id": 10
}

Hey @ico,

  1. Do I really need 2 mysql and 2 http query nodes or can I send the query dynamically depending on topic_id is sent or not?

I would use two different nodes like the way you did. This allows me to understand the data flow in the workflow better. This approach will also make debugging easy.

  1. I tried to add a merge node after the 2 sql calls but I could only add 1 http request but I failed

You can connect only one node to the output of the Merge node. Do you really need two different HTTP Request nodes? Looking at your workflow, both the nodes are identical. Did you try with a single node? Can you share what error you get, if any?

    1. the http request node returns only the last result - how can I merge the sql query result and the translation result?

Can you share some more information here? If the MySQL node returns 10 items, the HTTP Request node will process these 10 items and return you 10 translations.
You can use the Set node or the Function node to merge the outputs from the SQL Query node and the translation node.

Hi & Thanks

  1. ok make sense
  2. have one http request node without merge node - it is so easy - just did not think about it
  3. from mysql node i get back
[
    {
    "sqms_topic_id": 1,
    "name": "Topic with ID [1]",
    "sqms_role_id": 2
    },{...}, 
    {
    "sqms_topic_id": 19,
    "name": "Topic with ID [19]",
    "sqms_role_id": 6
    }
]

from the translate node I get back

[
    {
    "source_text": "Topic with ID [1]",
    "source_lang": "EN",
    "target_text": "Tema con ID [1]",
    "target_lang": "ES"
    },
    {...},
    {
    "source_text": "Topic with ID [19]",
    "source_lang": "EN",
    "target_text": "Tema con ID [19]",
    "target_lang": "ES"
    }
]

I would like to have

[
        {
        "sqms_topic_id": 1,
    	"source_text": "Topic with ID [1]",
    	"source_lang": "EN",
    	"target_text": "Tema con ID [1]",
    	"target_lang": "ES"
        "sqms_role_id": 2
        },        {...}, 
        {
        "sqms_topic_id": 19,
        "source_text": "Topic with ID [19]",
    	"source_lang": "EN",
    	"target_text": "Tema con ID [19]",
    	"target_lang": "ES"
    	"sqms_role_id": 3
        }
]

thanks Rob

hi Harshil

Answere to 3
is this the right video to watch?
n8n Nodemation basic - transforming json (3/3) (Tutorial)

I read Key Concepts | Docs
but since I’m not a programmer (only some php) I don’t know where to start … but an example is most of the time enought …

thsi could be a start - But 2 Have 2 Inputs, or?

Thanks rob

Hey Rob,

Did you try the Set node? You can specify the fields you want and only return these fields.

Since you have two SQL nodes connected to the HTTP Request node, and we need to take care of the condition when either of the nodes runs, you can add a NoOp node before the HTTP Request node. In the Set node, you can refer these values from the NoOp node.

1 Like

THANKS for your help - 1st workflow which might go to production is ready now!

3 Likes

That’s amazing! I am happy that I could help :sparkling_heart:

Have fun!

1 Like