# Passing multiple args to exec node

**URL:** <https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671>\
**Category:** General\
**Created:** [5 March 2020 19:00 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671 "2020-03-05T19:00:55Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![singhkpratham](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@singhkpratham](https://discourse.nodered.org/u/singhkpratham)\
**Post date:** [5 March 2020 19:00 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/1 "2020-03-05T19:00:55Z")

</div>

Hello,  
I have a python script, which expects 2 arguments. MQTT Topic and Payload.

Using exec node, I am able to send the 'Payload', but I also need to send msg.topic to the script.  
I have tried a lot, using msg.topic under the msg.payload option in exec node config, tried {{msg.topic}}, tried adding msg.topic directly in the command itself. Nothing has worked.  
So, is there a way to send 2 arguments?  
What I think can work is, use a node to merge msg.topic and msg.payload under msg.topic in a list/array. But change/join nodes have not been useful for these.  
I have nearly zero knowledge of javascript.

---

<div class="post-metadata">

**Author:** ![techraf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/techraf/32/17389_2.png) [@techraf](https://discourse.nodered.org/u/techraf)\
**Post date:** [5 March 2020 19:19 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/2 "2020-03-05T19:19:57Z")

</div>

If you don't have quotes in your `msg.payload` or `msg.topic`, then it sounds pretty easy. Just add a `template` node before the `exec` node and set `msg.payload` to:

```auto
"{{{payload}}}" "{{{topic}}}"

```

like this:

```auto
[{"id":"67e4cc00.8a08d4","type":"template","z":"ca1010aa.273f4","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\"{{payload}}\" \"{{topic}}\"","output":"str","x":700,"y":260,"wires":[["48ac40d.b5d42c"]]}]

```

* * *

> [@singhkpratham](#):
>
> But change/join nodes have not been useful for these.

`join` is for different purpose, but `change` fits well. Set `msg.payload` to JSONata:

```auto
'"' & msg.payload & '" "' & msg.topic & '"'

```

like this:

```auto
[{"id":"59c6553.283a4ac","type":"change","z":"ca1010aa.273f4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"'\"' & msg.payload & '\" \"' & msg.topic & '\"'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":180,"wires":[["8edfc0fb.b851a"]]}]

```

---

<div class="post-metadata">

**Author:** ![singhkpratham](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@singhkpratham](https://discourse.nodered.org/u/singhkpratham)\
**Post date:** [5 March 2020 19:42 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/3 "2020-03-05T19:42:45Z")

</div>

> [@techraf](#):
>
> "{{payload}}" "{{topic}}"

Thanks, I quickly built upon it and used 'parsed json' option at the bottom, and it has been successful. Still a small problem, as one of the inputs is 'mqtt topic', it has '/', which is leading to my outputs of template node like this:  
{'pld':ON, 'tpc': stat/strip/POWER2 }

Can you suggest how to deal with that?

---

<div class="post-metadata">

**Author:** ![singhkpratham](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@singhkpratham](https://discourse.nodered.org/u/singhkpratham)\
**Post date:** [5 March 2020 19:56 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/4 "2020-03-05T19:56:19Z")

</div>

> [@singhkpratham](#):
>
> {'pld':ON, 'tpc': stat/strip/POWER2 }

I am sorry it got printed like that.

`{'pld':ON, 'tpc': stat&#x2F;strip&#x2F;POWER2 }`

---

<div class="post-metadata">

**Author:** ![techraf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/techraf/32/17389_2.png) [@techraf](https://discourse.nodered.org/u/techraf)\
**Post date:** [5 March 2020 19:58 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/5 "2020-03-05T19:58:19Z")

</div>

So what exactly do you want to pass to the script? You asked:

> [@singhkpratham](#):
>
> is there a way to send 2 arguments?

But now it seems, you want a single argument with a JSON string...

* * *

You can prevent escaping forward slashes using triple moustaches `{{{topic}}}` in the template.

---

<div class="post-metadata">

**Author:** ![singhkpratham](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@singhkpratham](https://discourse.nodered.org/u/singhkpratham)\
**Post date:** [5 March 2020 20:04 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/6 "2020-03-05T20:04:35Z")

</div>

Earlier, the msg.payload had just one string. eg: "ON"

I wanted to send the topic as well as payload (coming from mqtt subscription).

So, now I have added both topic and payload to msg.payload. ie a JSON inside the msg.topic (at least this is the intention). Now this should get passed to the python script, where I will read it using "json.loads(xxx)", and parse both topic and payload and do some processing, and return my results by 'print(xxx)'.

---

<div class="post-metadata">

**Author:** ![techraf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/techraf/32/17389_2.png) [@techraf](https://discourse.nodered.org/u/techraf)\
**Post date:** [5 March 2020 20:17 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/7 "2020-03-05T20:17:50Z")

</div>

Ok, so you're good now, right?

---

<div class="post-metadata">

**Author:** ![singhkpratham](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@singhkpratham](https://discourse.nodered.org/u/singhkpratham)\
**Post date:** [5 March 2020 20:32 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/8 "2020-03-05T20:32:46Z")

</div>

The template I've written isnt getting passed like a JSON object, but rather weird string which python isnt able to read as JSON.

I will manage that part. Thanks a lot for your help, I literally spent 2 days and then finally took help of 'python-contrib-node', but it started throwing random errors.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [5 March 2020 21:17 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/9 "2020-03-05T21:17:53Z")

</div>

If you need it passed like a json object, that’s a different thing than passing multiple arguments. Which exactly do you need?

---

<div class="post-metadata">

**Author:** ![techraf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/techraf/32/17389_2.png) [@techraf](https://discourse.nodered.org/u/techraf)\
**Post date:** [5 March 2020 21:32 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/10 "2020-03-05T21:32:09Z")

</div>

> [@singhkpratham](#):
>
> The template I've written isnt getting passed like a JSON object, but rather weird string which python isnt able to read as JSON.

It's really not a rocket science. Google for `online json parser`, choose one, and verify if what you are passing is correct, because if you are trying:

```auto
{'pld':ON, 'tpc': stat/strip/POWER2 }

```

then no wonder it doesn't work.

And the template, I suspect you want, is as simple as:

```auto
'{ "pld": "{{{payload}}}", "tpc": "{{{topic}}}" }'

```

---

<div class="post-metadata">

**Author:** ![singhkpratham](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@singhkpratham](https://discourse.nodered.org/u/singhkpratham)\
**Post date:** [17 March 2020 14:11 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/11 "2020-03-17T14:11:47Z")

</div>

Apologies for late reply.

I actually needed multiple arguments itself. But figured that it is not possible to send multiple argument. Only a single json or arg can be passed.

So I decided to send a JSON which hold all my arguments.

Essentially, I am subscribing to a topic using mqtt, and then i want to send both the payload and topic to the python script

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 March 2020 14:41 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/12 "2020-03-17T14:41:40Z")

</div>

> [@singhkpratham](#):
>
> figured that it is not possible to send multiple argument

Yes it is, if you want to run a command `myscript` with args set to the topic and payload in a message then in a function node put

```auto
msg.payload = msg.topic + " " + msg.payload
return msg

```

then in the exec node tell it to execute the script and select the checkbox to add the payload to the message. It gets a little more complex if the parameters include spaces as then you would have surround each one with quotes.  
You can use a Change node rather than a Function node if you prefer.

---

<div class="post-metadata">

**Author:** ![singhkpratham](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@singhkpratham](https://discourse.nodered.org/u/singhkpratham)\
**Post date:** [17 March 2020 15:08 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/13 "2020-03-17T15:08:49Z")

</div>

Thanks, will try that too.

But again, this method is a workaround. It isnt exactly sending multiple args.  
Just being able to send both 'msg.topic' and 'msg.payload' would've been convenient.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 March 2020 15:20 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/14 "2020-03-17T15:20:45Z")

</div>

> [@singhkpratham](#):
>
> It isnt exactly sending multiple args.

When, in a terminal you type something like  
`myScript arg1 arg2`  
you are sending the command two arguments by putting the two args on the command line. To do the same with an exec node you put the command in the exec node and the two args in msg.payload. What is the difference?  
I think what you are asking for is to be able to source the two args from different properties of the message, which is a different question.

---

<div class="post-metadata">

**Author:** ![singhkpratham](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@singhkpratham](https://discourse.nodered.org/u/singhkpratham)\
**Post date:** [17 March 2020 15:35 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/15 "2020-03-17T15:35:56Z")

</div>

> [@Colin](#):
>
> source the two args from different properties of the message

Yeah, that's right. Would be a bit more convenient.  
Current solution works fine too.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [31 March 2020 15:36 UTC](https://discourse.nodered.org/t/passing-multiple-args-to-exec-node/22671/16 "2020-03-31T15:36:00Z")

</div>

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