# Change node: dynamically set a property

**URL:** <https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374>\
**Category:** General\
**Created:** [21 May 2019 15:17 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374 "2019-05-21T15:17:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andydesjardins](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andydesjardins/32/8556_2.png) [@Andydesjardins](https://discourse.nodered.org/u/Andydesjardins)\
**Post date:** [21 May 2019 15:17 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374/1 "2019-05-21T15:17:42Z")

</div>

Hello,

Is there a way to dynamically set a property in a change node?

Something like:

set flow.mode[msg.targetMode].enabled  
to msg.payload

I know I can do it in javascript, but it would be better with a change node.

Thank you

Andy

---

<div class="post-metadata">

**Author:** ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)\
**Post date:** [21 May 2019 15:23 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374/2 "2019-05-21T15:23:41Z")

</div>

This is possible with a jsonata expression ($eval) - here you can see an example:

> [@Allow target to be also a jsonata expression](https://discourse.nodered.org/t/allow-target-to-be-also-a-jsonata-expression/10510/5):
>
> In the source field i can enter something like this: $eval(payload[0].payload.cfg.p1,payload[1]) which returns a field in payload[1], defined by the string in .p1 (like "payload.data.val")

---

<div class="post-metadata">

**Author:** ![Andydesjardins](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andydesjardins/32/8556_2.png) [@Andydesjardins](https://discourse.nodered.org/u/Andydesjardins)\
**Post date:** [21 May 2019 15:47 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374/3 "2019-05-21T15:47:28Z")

</div>

Thanks for the quick answer.

I tried, but it not seams to work.

I try something like:  
on the for or on the msg: $eval(topic) or $eval(msg.topic) to msg.payload  
msg.topic containing "vip"  
msg.payload containing "hello"

It results: flow.$eval(topic) = "hello"

Do you have a more basic exemple? When I read the thread, is looks more like a wish than a real feature.

---

<div class="post-metadata">

**Author:** ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)\
**Post date:** [21 May 2019 15:58 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374/4 "2019-05-21T15:58:53Z")

</div>

Just show me what you tried (as exported flow). Also, which version of n-r do you have?

---

<div class="post-metadata">

**Author:** ![Andydesjardins](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andydesjardins/32/8556_2.png) [@Andydesjardins](https://discourse.nodered.org/u/Andydesjardins)\
**Post date:** [21 May 2019 16:58 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374/5 "2019-05-21T16:58:04Z")

</div>

We are on 0.20.3

Here's the flow:

`[{"id":"e9f00787.c7d228","type":"inject","z":"92c036c9.c74c08","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":480,"y":200,"wires":[["2d2b521.79b6fae"]]},{"id":"2d2b521.79b6fae","type":"change","z":"92c036c9.c74c08","name":"test","rules":[{"t":"set","p":"payload","pt":"msg","to":"hello","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"vip","tot":"str"},{"t":"set","p":"$eval(topic)","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":200,"wires":[["bc8ccf09.1f9f4"]]},{"id":"bc8ccf09.1f9f4","type":"debug","z":"92c036c9.c74c08","name":"dynamically set topic","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":910,"y":200,"wires":[]}]`

Thank you

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [21 May 2019 16:59 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374/6 "2019-05-21T16:59:25Z")

</div>

I wonder if eval is really needed? Is this what you want? Note that the key `targetMode` in the second rule is evaluated (not literal).

 ![p-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c8a23e72098084d6af30cb9130ddc347d42b44b8.png)

![p-02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/ef389391cbc931758acf996c7d56fa0adfc9a506.png)

---

<div class="post-metadata">

**Author:** ![Andydesjardins](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andydesjardins/32/8556_2.png) [@Andydesjardins](https://discourse.nodered.org/u/Andydesjardins)\
**Post date:** [21 May 2019 17:07 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374/7 "2019-05-21T17:07:47Z")

</div>

That won't do, because if the mode bbb already exists, with other properties, they get erased. The jsonata expression recreate the bbb property entirely instead of just targetting a sub-property.

Here's an exemple

```auto
[{"id":"e9f00787.c7d228","type":"inject","z":"92c036c9.c74c08","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":480,"y":200,"wires":[["2d2b521.79b6fae"]]},{"id":"2d2b521.79b6fae","type":"change","z":"92c036c9.c74c08","name":"test","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"vip","tot":"str"},{"t":"set","p":"mode.vip.name","pt":"msg","to":"vip","tot":"str"},{"t":"set","p":"mode","pt":"msg","to":"{topic: {\"enabled\": payload}}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":200,"wires":[["bc8ccf09.1f9f4"]]},{"id":"bc8ccf09.1f9f4","type":"debug","z":"92c036c9.c74c08","name":"dynamically set topic","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":910,"y":200,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)\
**Post date:** [21 May 2019 17:27 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374/8 "2019-05-21T17:27:34Z")

</div>

Simplified dynamic property example:

```auto
[{"id":"b585fa52.babdb8","type":"inject","z":"c421dfeb.3e752","name":"","topic":"lala","payload":"{\"topic\": \"testtopic\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":402,"y":3350,"wires":[["aaf0b982.837278","8cc8db27.ee65e8"]]},{"id":"aaf0b982.837278","type":"change","z":"c421dfeb.3e752","name":"test","rules":[{"t":"set","p":"cfg.topicfield","pt":"msg","to":"topic","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"$eval(cfg.topicfield,payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":611,"y":3355,"wires":[["8cc8db27.ee65e8"]]},{"id":"8cc8db27.ee65e8","type":"debug","z":"c421dfeb.3e752","name":"dynamically set topic","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":849,"y":3237,"wires":[]}]
```

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [21 May 2019 17:30 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374/9 "2019-05-21T17:30:36Z")

</div>

> [@Andydesjardins](#):
>
> That won't do, because if the mode bbb already exists, with other properties, they get erased. The jsonata expression recreate the bbb property entirely instead of just targetting a sub-property.

Indeed, fully agree.

---

<div class="post-metadata">

**Author:** ![Andydesjardins](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andydesjardins/32/8556_2.png) [@Andydesjardins](https://discourse.nodered.org/u/Andydesjardins)\
**Post date:** [21 May 2019 17:43 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374/10 "2019-05-21T17:43:28Z")

</div>

@moebius

I don't understand your flow sample. It seems it just take the property topicfield and us it as a value to overrite the topic with it. But the point would be to take the value of topicfield and use it as a dynamic reference to a propperty, so I could set the value with another property

topic = "lala"

msg.payload[topic].label = "the new value"

should result:

msg.payload.lala.label = "the new value"

All without recreating the lala object and preserving his properties.

---

<div class="post-metadata">

**Author:** ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)\
**Post date:** [21 May 2019 18:22 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374/11 "2019-05-21T18:22:20Z")

</div>

Well, it looks like you dont want to "dynamically **set** a property" as you wrote in the title of your thread...

Seems like you want to **create** a property dynamically.  
So you can support my feature request 🙂

Meanwhile, this is only possible with a function node afaik.

---

<div class="post-metadata">

**Author:** ![Andydesjardins](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andydesjardins/32/8556_2.png) [@Andydesjardins](https://discourse.nodered.org/u/Andydesjardins)\
**Post date:** [21 May 2019 18:23 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374/12 "2019-05-21T18:23:26Z")

</div>

Yes, I do want to do both! Create & set.

I'll support it.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [21 May 2019 19:04 UTC](https://discourse.nodered.org/t/change-node-dynamically-set-a-property/11374/13 "2019-05-21T19:04:43Z")

</div>

I got it working with JSONata `$merge` function but I have not tested thoroughly to be confident that it works well.

```auto
[{"id":"475c2554.e9f5bc","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"c79fba99.989f08","type":"inject","z":"475c2554.e9f5bc","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":260,"wires":[["8539380c.7095e8"]]},{"id":"8539380c.7095e8","type":"change","z":"475c2554.e9f5bc","name":"test","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"lala","tot":"str"},{"t":"set","p":"mode.lala.name","pt":"msg","to":"vip","tot":"str"},{"t":"set","p":"final","pt":"msg","to":"{\"mode\" : {topic : $merge ([$lookup(mode, topic) , {\"label\": \"new value\"}])}}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":260,"wires":[["5bbc8432.d8931c"]]},{"id":"5bbc8432.d8931c","type":"debug","z":"475c2554.e9f5bc","name":"dynamically set topic","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":590,"y":260,"wires":[]},{"id":"f81c9702.ccbf18","type":"inject","z":"475c2554.e9f5bc","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":120,"wires":[["b52b8741.c6fa78"]]},{"id":"b52b8741.c6fa78","type":"change","z":"475c2554.e9f5bc","name":"test","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"lala","tot":"str"},{"t":"set","p":"mode.lala.name","pt":"msg","to":"vip","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":120,"wires":[["8045059b.6421c8"]]},{"id":"8045059b.6421c8","type":"debug","z":"475c2554.e9f5bc","name":"dynamically set topic","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":590,"y":120,"wires":[]},{"id":"e728cba4.a57f08","type":"comment","z":"475c2554.e9f5bc","name":"Example","info":"","x":140,"y":80,"wires":[]},{"id":"340a83c9.74aa2c","type":"comment","z":"475c2554.e9f5bc","name":"Modified","info":"","x":140,"y":220,"wires":[]}]

```
