Extract a variable of a Json

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

Thank you,
Roger.

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 thingsScreen Shot 2020-12-17 at 11.15.13 AM

1 Like

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.

Thank you,
Roger.

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).

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.

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.

You can apply the JSON later

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

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.

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

What is other application?

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

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

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?

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

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

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
```

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
... 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

demo flow...

[{"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":[]}]

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

image

[{"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":""}]

Perfect Steve, lot's of thanks

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