Get value from JSON object and send to base64 node

#1

I currently have the following flow consisting of groups of nodes that all work in smaller flows, receiving a JSON string from TTN, checking what device we're talking about, and if it's the right one, then I want to convert the payload_raw value from base64 to hex and do a byteconversion to pull 10 values from the payload which is modbus with 10 registers. All 10 values must be grouped with the same timestamp and then inserted into MySQL so my table shows 10 device names that I define, with their value, and the same timestamp for all 10.

the byteconversion works, inserting the SQL works, these are all recycled bits from existing flows, but how do I extract 'payload_raw' from the json node, and send it to the base64 node, and how do I retain the timestamp until the very end of the flow? In other words, what code do I use in my empty 'parse' function node?

afbeelding

0 Likes

#2

If you read the info panel for the Base64 node it will tell you the path often data it will convert.
(usually msg.payload but check) so if you have other things store in msg.payload.something
you need to move them out before you convert ( and move the right bit to msg.payload)

You can use the change node to Move msg.payload.something to msg.something etc

1 Like

#3

Thanks, the change node is exactly what I needed, it all works now.

0 Likes

#4

Well, ukmoose already explained how to ensure you extract the correct data from the json. Based on the picture you posted I presume that the properties you need to extract are: msg.payload_raw and msg.metadata.time.

I created a small flow to fake your data, extract the interesting information from the JSON data and decode the base64 to hex.

Good reference on how to decode base64 the simple way without using a contrib node (tks shrickus).`

If it works for you then it is missing only to handle the time information.

Flow:

[{"id":"d140ec3e.aed54","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"180b562c.4ffb3a","type":"inject","z":"d140ec3e.aed54","name":"{ }","topic":"","payload":"{}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":180,"wires":[["196e91a1.312f8e"]]},{"id":"196e91a1.312f8e","type":"change","z":"d140ec3e.aed54","name":"Fake Dataset","rules":[{"t":"set","p":"payload_raw","pt":"msg","to":"/8BEhEP7Q7REQkUSQ5NEIUNDQspEZA==","tot":"str"},{"t":"set","p":"metadata.time","pt":"msg","to":"$now()","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":180,"wires":[["5b344ff2.10fdd"]]},{"id":"5b344ff2.10fdd","type":"function","z":"d140ec3e.aed54","name":"Decode Base64","func":"let encoded = msg.payload_raw;\n//encoded = Buffer.from(msg.payload).toString(\"base64\");\n\nmsg.decoded = Buffer.from(encoded, \"base64\");\n\n\nreturn msg;","outputs":1,"noerr":0,"x":480,"y":180,"wires":[["723343de.fbb5bc"]]},{"id":"723343de.fbb5bc","type":"debug","z":"d140ec3e.aed54","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":180,"wires":[]}]
0 Likes

#5

Cool, but node-red-node-base64 isn't a contrib...

0 Likes

#6

Indeed, as a matter of fact I was typing a (long) response and posted without reading the intermediate post. I was not referring specifically to node-red-node-base64 .
Edit: I guess I jumped to quickly to coding. The flow in the OP was already using a base64 node (it is showing too small in my already small screen), which is a great node. No need to make things more complicated using JS functions for the conversion. I apologize the confusion.

0 Likes