# Change node {} syntax

**URL:** <https://discourse.nodered.org/t/change-node-syntax/66315>\
**Category:** General\
**Created:** [11 August 2022 11:03 UTC](https://discourse.nodered.org/t/change-node-syntax/66315 "2022-08-11T11:03:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [11 August 2022 11:03 UTC](https://discourse.nodered.org/t/change-node-syntax/66315/1 "2022-08-11T11:03:38Z")

</div>

I want to change msg.payload from a single value to a javascript object.

In a function it's one line  
msg.payload = { temperature: msg.payload};

In a change node I can do it like this, which is very inelegant

 ![Untitled 6](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/0/601794627880f9d428a0b49503e94c92782287a0.jpeg)

No doubt it can be done with a J: expression - a system so opaque it makes regular expressions seem transparent! 🤬  
But can it be done with a single Set {} line?

What is the _right_ low-code way to achieve this change?

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [11 August 2022 11:08 UTC](https://discourse.nodered.org/t/change-node-syntax/66315/2 "2022-08-11T11:08:55Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/22a79cacd6bd030b2b7a4de4d7092e7f4bc3e0ff.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [11 August 2022 11:14 UTC](https://discourse.nodered.org/t/change-node-syntax/66315/3 "2022-08-11T11:14:08Z")

</div>

Ah. That explains why the Move option exists! 😀

But it's not applicable to a more complex situation eg transforming [120, 95] to {temperature: 120, humidity: 95} ?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 August 2022 11:20 UTC](https://discourse.nodered.org/t/change-node-syntax/66315/4 "2022-08-11T11:20:55Z")

</div>

try this

```auto
[{"id":"f1d72adafbd635b7","type":"change","z":"00a35f2f631d08be","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"t\":payload[0], \"h\":payload[1]}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":540,"wires":[["5a8401db3a20ab69"]]}]

```

you need to expand the names.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [11 August 2022 11:31 UTC](https://discourse.nodered.org/t/change-node-syntax/66315/5 "2022-08-11T11:31:42Z")

</div>

Every time I see struggling with such data sources where output is just an array of numbers I just need to ask - can't it be improved.  
Because of -  
Can you trust the data to :

- be always an array
- always having same amount of elements
- always having elements in same order

If you can, fine. If not - it is seriously source of (mostly hard to find and fix) failures.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [11 August 2022 11:45 UTC](https://discourse.nodered.org/t/change-node-syntax/66315/6 "2022-08-11T11:45:32Z")

</div>

Thanks for the J: solution Paul. I recognise J's utility but I can't warm to it...

hotNipi makes a good point that the incoming message shouldn't really be just a value or an array of values.  
What I am actually trying to do is construct a mariadb prepared query. My selection criteria come from a dropdown which returns just the value - "January".  
I could use a Change node to transform this to an object - {month: "January"} but ... [GOTO Line 1] 🙃

Edit - OK I can make the dropdown return a string {"month":"January"} and pass it through a Json parser node.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 August 2022 13:29 UTC](https://discourse.nodered.org/t/change-node-syntax/66315/7 "2022-08-11T13:29:41Z")

</div>

> [@jbudd](#):
>
> But can it be done with a single Set {} line?

Not quite but it can be done trivially (and a lot more of course) by using JSONata.

> [@jbudd](#):
>
> No doubt it can be done with a J: expression - a system so opaque it makes regular expressions seem transparent!

Not really for something like this. It really would be as simple as you envisage it.

> [@jbudd](#):
>
> But it's not applicable to a more complex situation eg transforming [120, 95] to {temperature: 120, humidity: 95} ?

Nope but jsonata something like:

```auto
{"temperature": payload[0], "humidity": payload[1]}

```

will probably work (didn't actually try it but i think it would work like that).

---

<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:** [10 October 2022 13:29 UTC](https://discourse.nodered.org/t/change-node-syntax/66315/8 "2022-10-10T13:29:50Z")

</div>

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