Sort Json Object variables using a function

Hello guys,
I want to use a function node with several outputs to sort each variables contained in the object.
Still Can't get it to work. any help appreciated.

{"topic":"/v1.6/devices/MB10","payload":{"MB10":{"value":0,"id":1,"name":"MB10","MotorProteccAcc":0,"LoadPercent":0,"OutputVolts":0,"DcbusVolts":332,"EnergyMeter":10.13,"Analog1%":84.01,"StackTemp":50,"InverterTemp":50,"I/OWord":2,"LastTrip":26,"StatusWord":5,"AppReg1811":40,"AppReg1812":48,"PresetRef8":38.08,"PostRampRef":0,"CurrentMag":0,"OutputFreq":0,"Reg20.21":40000,"Reg20.22":16000}},"qos":0,"retain":false,"_msgid":"4eb3d84.a60bb28"}

I thinking function node similar to this one below ( this sort msg to different outputs based on the topics)


Help and ideas would be appreciated.

You don't need a function node for that, you would be better off with a switch node.

Other than that, it looks OK. You don't need the default since not returning anything at all is the same as returning all nulls. Also your example msg wouldn't return anything as the topic is not in the list.

@TotallyInformation , my first attempt was using the switch node.. but it doesn't works, posted that question on Slack node-red.
this was the question that I posted yesterday in slack.

" hello guys, I am trying to use switch node to separate data inside an object, I am not sure what msg property do I have to use to compare against . Any help appreciated"


You have used a different path altogether there.

You should have msg.topic as the "Property" and then the full topic string with "==" in the tests.

I used in the past the switch node to sort incoming MQTT msg based on the topic , but this not the case now.
what I am trying to do is function with several outputs each output for the variables values contained in the object.

Hi @luisgcu

from the screenshot of slack you can see I and others provided some suggestions, a couple replies later you said "that worked"....

so what exactly did work? Why have you asked the same question again? What different answer are you hoping to receive?

You are looking for something like this, but preferably want to contain it in 1 node (switch or function node). I tried the expressions, but am no hero with jsonata in a switch node.

[{"id":"80de6bc7.fc71e8","type":"function","z":"821b7650.0cd478","name":"input data","func":"input = {\"topic\":\"/v1.6/devices/MB10\",\"payload\":{\"MB10\":{\"value\":0,\"id\":1,\"name\":\"MB10\",\"MotorProteccAcc\":0,\"LoadPercent\":0,\"OutputVolts\":0,\"DcbusVolts\":332,\"EnergyMeter\":10.13,\"Analog1%\":84.01,\"StackTemp\":50,\"InverterTemp\":50,\"I/OWord\":2,\"LastTrip\":26,\"StatusWord\":5,\"AppReg1811\":40,\"AppReg1812\":48,\"PresetRef8\":38.08,\"PostRampRef\":0,\"CurrentMag\":0,\"OutputFreq\":0,\"Reg20.21\":40000,\"Reg20.22\":16000}},\"qos\":0,\"retain\":false,\"_msgid\":\"4eb3d84.a60bb28\"}\n\nreturn input;","outputs":1,"noerr":0,"x":258,"y":220,"wires":[["2a40b491.ef0bac"]]},{"id":"22308fa7.5de6d8","type":"debug","z":"821b7650.0cd478","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":776,"y":176,"wires":[]},{"id":"9d224c2f.b0d718","type":"inject","z":"821b7650.0cd478","name":"Go","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":116,"y":220,"wires":[["80de6bc7.fc71e8"]]},{"id":"2a40b491.ef0bac","type":"switch","z":"821b7650.0cd478","name":"switch topic","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"/v1.6/devices/MB10","vt":"str"},{"t":"eq","v":"/v1.6/devices/IOT_ID10","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":422,"y":198,"wires":[["545e54aa.a6a1a4","1d62c258.5ebcb6"],[]]},{"id":"2bdbcf31.bcaa","type":"debug","z":"821b7650.0cd478","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":776,"y":220,"wires":[]},{"id":"545e54aa.a6a1a4","type":"change","z":"821b7650.0cd478","name":"Energy meter","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.MB10.EnergyMeter","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":598,"y":132,"wires":[["22308fa7.5de6d8"]]},{"id":"1d62c258.5ebcb6","type":"change","z":"821b7650.0cd478","name":"DcbusVolts","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.MB10.DcbusVolts","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":598,"y":176,"wires":[["2bdbcf31.bcaa"]]}]

Lets give a try again..
first what worked for me was to display the value of objects variables directly on dashboard node , that works.. But that is not what really need..
Why not?, because would be using those variables in other program sections No only dashboard elements that's why I would prefer to create function node with one output for each variable ,creating sort of global variables ( using link out node)
See an hypothetical example below

@bakman2 that works ! and is good way, but what about to do the same in just one function node? in just one unique block, instead of repeating several switch nodes for each variable?

You can do it like this:

[{"id":"80de6bc7.fc71e8","type":"function","z":"821b7650.0cd478","name":"input data","func":"input = {\"topic\":\"/v1.6/devices/MB10\",\"payload\":{\"MB10\":{\"value\":0,\"id\":1,\"name\":\"MB10\",\"MotorProteccAcc\":0,\"LoadPercent\":0,\"OutputVolts\":0,\"DcbusVolts\":332,\"EnergyMeter\":10.13,\"Analog1%\":84.01,\"StackTemp\":50,\"InverterTemp\":50,\"I/OWord\":2,\"LastTrip\":26,\"StatusWord\":5,\"AppReg1811\":40,\"AppReg1812\":48,\"PresetRef8\":38.08,\"PostRampRef\":0,\"CurrentMag\":0,\"OutputFreq\":0,\"Reg20.21\":40000,\"Reg20.22\":16000}},\"qos\":0,\"retain\":false,\"_msgid\":\"4eb3d84.a60bb28\"}\n\nreturn input;","outputs":1,"noerr":0,"x":258,"y":308,"wires":[["329cc654.d3e8ba"]]},{"id":"9d224c2f.b0d718","type":"inject","z":"821b7650.0cd478","name":"Go","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":116,"y":308,"wires":[["80de6bc7.fc71e8"]]},{"id":"329cc654.d3e8ba","type":"function","z":"821b7650.0cd478","name":"","func":"i = msg.payload.MB10\n\nv1 = {payload:i.DcbusVolts}\nv2 = {payload:i.EnergyMeter}\n\n\n\nreturn [v1,v2];","outputs":2,"noerr":0,"x":402,"y":308,"wires":[["fd912678.bc961"],["d4c40a2b.7f6c6"]]},{"id":"fd912678.bc961","type":"debug","z":"821b7650.0cd478","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":534,"y":286,"wires":[]},{"id":"d4c40a2b.7f6c6","type":"debug","z":"821b7650.0cd478","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":534,"y":330,"wires":[]}]

there u go!.. :heart_eyes: that was exactly what I want to do!! thanks you @bakman2 and thanks all for always trying to help the miserable souls not experts in JavaScript!!

I have i similar problem I need help to.
I get this Json from an API and i like to sort it and save it in fire base.
But I cant figure out how to make the function code.
Can someone please help me out here.
I need the Hande, address, longotude and latitude and phone to store seperate in firebase under the Handle as the head and the other info as childs.
Hoping for help soon thank you all and be safe out there......
Martin

Please don't cross-post.