# How get and route a single pair of key/value from an object?

**URL:** <https://discourse.nodered.org/t/how-get-and-route-a-single-pair-of-key-value-from-an-object/45512>\
**Category:** General\
**Created:** [11 May 2021 20:45 UTC](https://discourse.nodered.org/t/how-get-and-route-a-single-pair-of-key-value-from-an-object/45512 "2021-05-11T20:45:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Topolynx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/topolynx/32/32597_2.png) [@Topolynx](https://discourse.nodered.org/u/Topolynx)\
**Post date:** [11 May 2021 20:45 UTC](https://discourse.nodered.org/t/how-get-and-route-a-single-pair-of-key-value-from-an-object/45512/1 "2021-05-11T20:45:43Z")

</div>

Hi. I'm moving my first steps in node-red, and I'm facing a lot of problems since the template flow that I need to use for my needs contains a lot of stuff that I still don't understand.  
My first need is to create some objects that contains each one the values of the object "dps" that I have retrieved within a payload:

```auto
{"devId":"228780332cf432062e35","dps":{"1":false,"2":15,"3":24,"7":false,"10":"0","11":0,"101":"0","102":false}}

```

I've checked the path of the keys, for example payload.dps["1"] but how could I obtain a pair of key/value to send via mqtt?  
Thank you

---

<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:** [11 May 2021 20:58 UTC](https://discourse.nodered.org/t/how-get-and-route-a-single-pair-of-key-value-from-an-object/45512/2 "2021-05-11T20:58:58Z")

</div>

> [@Topolynx](#):
>
> how could I obtain a pair of key/value

I am sorry, I don't understand exactly what you need to send to MQTT. Can you give us an example of exactly what you want to send?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [11 May 2021 21:02 UTC](https://discourse.nodered.org/t/how-get-and-route-a-single-pair-of-key-value-from-an-object/45512/3 "2021-05-11T21:02:02Z")

</div>

> [@Topolynx](#):
>
> I've checked the path of the keys, for example payload.dps["1"] but how could I obtain a pair of key/value to send via mqtt?

You can create an object with key/value like this in a function node...

```auto
msg.payload = {
  key: "value"
}

```

so if you want the value of dps in a key/val you would...

```auto
msg.payload = {
  dps: msg.payload.dps["1"]
}

```

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

**Author:** ![Topolynx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/topolynx/32/32597_2.png) [@Topolynx](https://discourse.nodered.org/u/Topolynx)\
**Post date:** [12 May 2021 06:00 UTC](https://discourse.nodered.org/t/how-get-and-route-a-single-pair-of-key-value-from-an-object/45512/4 "2021-05-12T06:00:25Z")

</div>

Of course @Colin: I need to control locally a tuya appliance that I can't integrate in home Assistant with the [LocalTuya](https://github.com/rospogrigio/localtuya) custom component.  
With the [node-red-contrib-tuya-local](https://github.com/subzero79/node-red-contrib-tuya-local) node I can control that appliance, and my main idea is to use MQTT as a bridge between Node-Red and HA, and to create a climate device based on MQTT.  
The node's payload `{"devId":"228780332cf432062e35","dps":{"1":false,"2":15,"3":24,"7":false,"10":"0","11":0,"101":"0","102":false}}` showed me the dps keys/values that I should send to MQTT, for example the `payload.dps["7"]` routed to something like `"/tuya/bimar_local/swing/"` (that is a nice example, since I can't control that function with HA), and so on.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 May 2021 07:08 UTC](https://discourse.nodered.org/t/how-get-and-route-a-single-pair-of-key-value-from-an-object/45512/5 "2021-05-12T07:08:03Z")

</div>

> [@Topolynx](#):
>
> for example the `payload.dps["7"]` routed to something like `"/tuya/bimar_local/swing/"`

Use a change node

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/91588270b7d6a17ea65dc1d9ae5cf390f1006cea.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cbb0069f2e75c6f6c9b360c7831b27348d6a32e9.png)

**NOTE: topics should never start and end with a `/` slash.**

---

<div class="post-metadata">

**Author:** ![Topolynx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/topolynx/32/32597_2.png) [@Topolynx](https://discourse.nodered.org/u/Topolynx)\
**Post date:** [12 May 2021 07:20 UTC](https://discourse.nodered.org/t/how-get-and-route-a-single-pair-of-key-value-from-an-object/45512/6 "2021-05-12T07:20:47Z")

</div>

Thank you, that worked like a charm!  
Looking around I've found the way to convert also the value of my key:

```auto
msg.payload = {
  power: msg.payload.dps["1"] ? "on" : "off"
}
return msg;

```

Is that language JavaScript?

---

<div class="post-metadata">

**Author:** ![Topolynx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/topolynx/32/32597_2.png) [@Topolynx](https://discourse.nodered.org/u/Topolynx)\
**Post date:** [12 May 2021 07:45 UTC](https://discourse.nodered.org/t/how-get-and-route-a-single-pair-of-key-value-from-an-object/45512/7 "2021-05-12T07:45:19Z")

</div>

Sorry I've replied before reading your post.  
Is it possible to set not only the key 7 as msg.payload but also convert its value from boolean to "on" and "off"?  
I'm trying to obtain that with the expression of my previous reply but without success. I imagine that it happens because I should write in JSonata?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 May 2021 07:48 UTC](https://discourse.nodered.org/t/how-get-and-route-a-single-pair-of-key-value-from-an-object/45512/8 "2021-05-12T07:48:06Z")

</div>

Just use a function node & set the payload and topic as desired...

```auto
msg.payload = {
  power: msg.payload.dps["1"] ? "on" : "off"
}
msg.topic ="tuya/bimar_local/swing";
return msg;

```

Ps, yes, it is JavaScript.

---

<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:** [11 July 2021 07:48 UTC](https://discourse.nodered.org/t/how-get-and-route-a-single-pair-of-key-value-from-an-object/45512/9 "2021-07-11T07:48:25Z")

</div>

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