# Extract a variable of a Json

**URL:** <https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660>\
**Category:** General\
**Created:** [17 December 2020 16:05 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660 "2020-12-17T16:05:53Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [17 December 2020 16:05 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/1 "2020-12-17T16:05:53Z")

</div>

Hello, I have a JSON with diferent variables in my node-red and I need to extract one of this variables at other node-red, somebody know how I can do it?

I attach the picture of the variables, I need to extract in\_temp.  
 ![Captura](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b1bb5b5980d68547d94b3204f8623b1abdc68423.png)

Thank you,  
Roger.

---

<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:** [17 December 2020 16:15 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/2 "2020-12-17T16:15:37Z")

</div>

If you feed the outpput of that node to d debug node, one of the options in the debug panel is to copy the path to an item. That makes it easy to figure out how to extract things ![Screen Shot 2020-12-17 at 11.15.13 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d45acab514ed7437ef216d5ddc61ffa754f4cc0d.png)

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [17 December 2020 16:42 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/3 "2020-12-17T16:42:21Z")

</div>

Hi zenofmud,

I think that maybe I explained it bad. Before image that I put before is from a function node, and I cannot apply the solution you explained. I add a new picture to see better my structure, function, json and the debug of the data after the json node.

 ![Captura2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9db748a80ef2745d2a06f6b3d7fdce657dc61aa8.png)

Thank you,  
Roger.

---

<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:** [17 December 2020 17:11 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/5 "2020-12-17T17:11:30Z")

</div>

By the look of it, you send a JS object out of the function THEN through a JSON node (turning the js object into a String of JSON).

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

why are you doing that?

If you want to access in\_temp, then remove the JSON node & do as @zenofmud says & just copy the path to the object property.

And it would pay you to read up on JS vs JSON.

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [17 December 2020 19:37 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/6 "2020-12-17T19:37:27Z")

</div>

Well, I understand your idea and it's a good option. I convert the object to string because the final scada aplication where I want to represent the data needs to receive it as a string. Now I'm doing tests representing it in other ways, and as zenofmud and you are saying I can apply this option, also I'm interested to do it from the object.

Thank you.

---

<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:** [17 December 2020 20:13 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/7 "2020-12-17T20:13:20Z")

</div>

You can apply the JSON later

---

<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 December 2020 20:14 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/8 "2020-12-17T20:14:07Z")

</div>

Do everything with the data as objects and then, if necessary, convert it to JSON at the end.

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [18 December 2020 16:56 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/9 "2020-12-18T16:56:44Z")

</div>

I tryed it and it's not possible to transmit data as an object, I need to do it as a string. When I say transmit I refer from Node-red to other aplication.

---

<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:** [18 December 2020 17:22 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/10 "2020-12-18T17:22:02Z")

</div>

Your original question was...

> I have a JSON with diferent variables in my node-red and I need to extract one of this variables at other node-red, somebody know how I can do it?

So we said "dont change to JSON"

Now you say

> [@Roger](#):
>
> I need to do it as a string. When I say transmit I refer from Node-red to other aplication.

What is other application?

How are you transmitting between node-red --\> other application?

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [23 December 2020 16:15 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/11 "2020-12-23T16:15:09Z")

</div>

Hi Steve, sorry for the delay. I'm doing similar things in two ways and for this sometimes I'm not enough specific.

Finally I can achieve my objective using an object, as everybody in this conversation says.

Thanks to all,  
Roger

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [23 December 2020 16:18 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/12 "2020-12-23T16:18:31Z")

</div>

One new question before oppening a new forum. In my object there are two undefined variables, time and devEUI due to are variables a part of the function I attach messages before. Somebody knows how I can add or display correctly this variables?

---

<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:** [23 December 2020 16:32 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/13 "2020-12-23T16:32:58Z")

</div>

> [@Roger](#):
>
> Somebody knows how I can add or display correctly this variables?

show us what you mean - perhaps a screenshot with comments about what the value _SHOULD_ be and where the value comes from?

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [24 December 2020 08:31 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/14 "2020-12-24T08:31:13Z")

</div>

Sorry Steve,

Well, I'm receiving data from a multitech gateway, exactly an hexadecimal message, this gateway collects the data from sensors and this data is two temperatures (external and internal), humidity, battery status and others related to the sensor (can see on the pictures below). Also apart from this hexadecimal message there is the devEUI and time, is in this two values where I have the problem, cause I don't know how to identify him to see at the message object, cause the other variables I substract from the hexadecimal message, where every character has his own significate.

I supose that there is some way to add this in the object in the function that I use, but I don't know what. I attach the function that I'm using where you can see exactly what I'm say, and a picture from my debug message.

FUNCTION: {  
datoHex = datoHex + "0" + msg.payload[i].toString(16);  
}  
else  
datoHex = datoHex + msg.payload[i].toString(16);  
}

BAT\_status = (parseInt(datoHex.substring(0, 4), 16)\>\>14)&0xFF;

Built\_In\_Temperature = parseInt(datoHex.substring(4,8), 16);  
Built\_In\_Humidity = parseInt(datoHex.substring(8,12), 16)/10.0;  
Ext = datoHex.substring(12,14); //defines the external sensor model  
Ext\_value = parseInt(datoHex.substring(14,18), 16); //value for external sensor value

if(Built\_In\_Temperature&0x8000)  
Built\_In\_Temperature = (Built\_In\_Temperature)-0x10000;

Built\_In\_Temperature = Built\_In\_Temperature/100.0;

if(Ext\_value&0x8000)  
Ext\_value = (Ext\_value)-0x10000;

Ext\_value = Ext\_value/100.0;

msg =  
{  
"payload":  
{  
"deveui":msg.deveui,  
"time":msg.time,  
//"data":datoHex,  
//"cnt\_seq":msg.seqn,  
"bat\_status":BAT\_status,  
"in\_temp":Built\_In\_Temperature,  
"in\_hum":Built\_In\_Humidity,  
//"ext":Ext,  
"ext\_temp":Ext\_value  
}  
};

return msg;

PICTURE:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/036724559c2ac6d0858b843200c43739be5f97ba.png)

---

<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:** [24 December 2020 09:11 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/15 "2020-12-24T09:11:27Z")

</div>

Hi, firstly, your code it fairly unreadable - it is always best to use the code button or surround any code with back ticks

` ````  
` like this`  
`````

> [@Roger](#):
>
> Well, I'm receiving data from a multitech gateway, exactly an hexadecimal message, this gateway collects the data from sensors and this data is two temperatures (external and internal), humidity, battery status and others related to the sensor (can see on the pictures below). Also apart from this hexadecimal message there is the devEUI and time, is in this two values where I have the problem, cause I don't know how to identify him to see at the message object,

I dont see these `devEUI` and `time` values in your code or screenshot - can you post a screenshot from a debug node set to "show complete msg" (note also, above you say `devEUI` but in your function code you wrote `deveui` (JavaScript is CASE SENSITIVE). It is always better to copy & paste the variable like this...  
 ![iv5YFiCRJU](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/4/64ff716d8646964c17b74308bbbed33b77812243.gif)  
... to avoid typos

* * *

some additional info.

there is a node called node-red-contrib-buffer-parser that can greatly simplify the task of buffer/hex type data conversion - example...

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

demo flow...

```auto
[{"id":"c7ac62e4.4ff62","type":"inject","z":"64ce1d00.d049e4","name":"","props":[{"p":"payloadStr","v":"cbcf090f016f0109277fff","vt":"str"},{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[203,207,9,15,1,111,1,9,39,127,255]","payloadType":"bin","x":1730,"y":360,"wires":[["7b56e862.51fd78"]]},{"id":"7b56e862.51fd78","type":"buffer-parser","z":"64ce1d00.d049e4","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"uint16be","name":"BAT_status","offset":0,"length":1,"offsetbit":0,"scale":">> 14","mask":""},{"type":"int16be","name":"Built_In_Temperature","offset":2,"length":1,"offsetbit":0,"scale":"/100.0","mask":""},{"type":"uint16be","name":"Built_In_Humidity","offset":4,"length":1,"offsetbit":0,"scale":"/10.0","mask":""},{"type":"hex","name":"Ext","offset":6,"length":1,"offsetbit":0,"scale":"/10.0","mask":""},{"type":"int16be","name":"Ext_value","offset":7,"length":1,"offsetbit":0,"scale":"/100.0","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1730,"y":420,"wires":[["99ab91b9.5201b"]]},{"id":"99ab91b9.5201b","type":"debug","z":"64ce1d00.d049e4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1750,"y":480,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [24 December 2020 09:51 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/16 "2020-12-24T09:51:12Z")

</div>

It's very strange what happens to me. Really I think it should run correctlly without any more node

 ![Capturaforum](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a736932a59cb4962b6a8c411e2c70a70bca5bb86.png)

---

<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:** [24 December 2020 10:13 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/17 "2020-12-24T10:13:28Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/3692ecccbea023c068991ca9deeadcb9b4203f38.jpeg)

---

<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:** [24 December 2020 10:20 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/18 "2020-12-24T10:20:04Z")

</div>

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

```auto
[{"id":"d892e0ef.65fe8","type":"inject","z":"64ce1d00.d049e4","name":"Lora Data","props":[{"p":"payloadStr","v":"cbcf090f016f0109277fff","vt":"str"},{"p":"deveui","v":"ab:cd:ef:01:02:03","vt":"str"},{"p":"payload"},{"p":"time","v":"2020-21-12T10:15Z","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[203,207,9,15,1,111,1,9,39,127,255]","payloadType":"bin","x":1960,"y":140,"wires":[["952f2918.cca728"]]},{"id":"952f2918.cca728","type":"buffer-parser","z":"64ce1d00.d049e4","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"uint16be","name":"BAT_status","offset":0,"length":1,"offsetbit":0,"scale":">> 14","mask":""},{"type":"int16be","name":"Built_In_Temperature","offset":2,"length":1,"offsetbit":0,"scale":"/100.0","mask":""},{"type":"uint16be","name":"Built_In_Humidity","offset":4,"length":1,"offsetbit":0,"scale":"/10.0","mask":""},{"type":"hex","name":"Ext","offset":6,"length":1,"offsetbit":0,"scale":"/10.0","mask":""},{"type":"int16be","name":"Ext_value","offset":7,"length":1,"offsetbit":0,"scale":"/100.0","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":2110,"y":140,"wires":[["1498b470.89c12c"]]},{"id":"2e4f8722.5eddd8","type":"mqtt out","z":"64ce1d00.d049e4","name":"","topic":"lora/data","qos":"","retain":"","broker":"dc811dae.05f5","x":2180,"y":200,"wires":[]},{"id":"650c34a1.591e7c","type":"mqtt in","z":"64ce1d00.d049e4","name":"","topic":"lora/data","qos":"2","datatype":"json","broker":"dc811dae.05f5","x":1960,"y":300,"wires":[["271574a8.4c206c"]]},{"id":"1498b470.89c12c","type":"change","z":"64ce1d00.d049e4","name":"add deveui and time","rules":[{"t":"set","p":"payload.time","pt":"msg","to":"time","tot":"msg"},{"t":"set","p":"payload.deveui","pt":"msg","to":"deveui","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2000,"y":200,"wires":[["2e4f8722.5eddd8"]]},{"id":"271574a8.4c206c","type":"debug","z":"64ce1d00.d049e4","name":"to InfluxDB","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2120,"y":300,"wires":[]},{"id":"4c6afb2c.8262c4","type":"comment","z":"64ce1d00.d049e4","name":"flow 1","info":"","x":1950,"y":100,"wires":[]},{"id":"a3ab7eaf.1a472","type":"comment","z":"64ce1d00.d049e4","name":"flow 2","info":"","x":1950,"y":260,"wires":[]},{"id":"dc811dae.05f5","type":"mqtt-broker","name":"","broker":"broker.emqx.io","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"50","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [24 December 2020 10:23 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/19 "2020-12-24T10:23:45Z")

</div>

Perfect Steve, lot's of thanks

---

<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:** [22 February 2021 10:23 UTC](https://discourse.nodered.org/t/extract-a-variable-of-a-json/37660/20 "2021-02-22T10:23:45Z")

</div>

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