How to extract values from a json object

Hi.

I have a json object that I want to extract some value from, but I cant' find the way to do it.

I 'm able to see the entire object in the debug window, but I'm not able to extract what I need and then send it to different dashboard.
Here is what I tried till now :

[{"id":"a5b85e10.ae3ac8","type":"debug","z":"d9e4a171.78ed68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":120,"wires":[]},{"id":"eabf1257.2c0ba","type":"mqtt in","z":"d9e4a171.78ed68","name":"","topic":"/sbms","qos":"2","datatype":"auto","broker":"59a61781.6bb818","x":100,"y":120,"wires":[["96c2d78f.4382e"]]},{"id":"96c2d78f.4382e","type":"json","z":"d9e4a171.78ed68","name":"","property":"payload","action":"","pretty":false,"x":280,"y":120,"wires":[["2a49038.1c9e57c","a5b85e10.ae3ac8"]]},{"id":"2a49038.1c9e57c","type":"switch","z":"d9e4a171.78ed68","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"\"time\"","vt":"str"},{"t":"eq","v":"payload{\"time\":{\"year\":0,\"month\":1,\"day\":3,\"hour\":5,\"minute\":33,\"second\":16},\"soc\":40,\"cellsMV\":[3230,3201,3224,3229,3231,3223,3223,3228],\"tempInt\":20.3,\"tempExt\":18.8,\"currentMA\":{\"battery\":-2037,\"pv1\":32,\"pv2\":0,\"extLoad\":0},\"ad2\":0,\"ad3\":0,\"ad4\":0,\"heat1\":0,\"heat2\":0,\"flags\":{\"OV\":false,\"OVLK\":false,\"UV\":false,\"UVLK\":false,\"IOT\":false,\"COC\":false,\"DOC\":false,\"DSC\":false,\"CELF\":false,\"OPEN\":false,\"LVC\":false,\"ECCF\":false,\"CFET\":true,\"EOC\":false,\"DFET\":true,\"delta\":30}}","vt":"str"},{"t":"eq","v":"\"cellsMV\"","vt":"str"},{"t":"eq","v":"\"tempInt\"","vt":"str"},{"t":"eq","v":"tempExt","vt":"str"},{"t":"eq","v":"currentMA","vt":"str"}],"checkall":"true","repair":false,"outputs":6,"x":380,"y":200,"wires":[["558bc663.11115"],["83607171.b61ed","3710071e.e172a"],[],["9383e093.ab13d","7ec6d3d5.448c4c"],["fd4f5fe4.2a054","bd5a1d62.773c9"],[]]},{"id":"3710071e.e172a","type":"debug","z":"d9e4a171.78ed68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":160,"wires":[]},{"id":"7ec6d3d5.448c4c","type":"debug","z":"d9e4a171.78ed68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":200,"wires":[]},{"id":"bd5a1d62.773c9","type":"debug","z":"d9e4a171.78ed68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":240,"wires":[]},{"id":"558bc663.11115","type":"debug","z":"d9e4a171.78ed68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":140,"wires":[]},{"id":"83607171.b61ed","type":"ui_gauge","z":"d9e4a171.78ed68","name":"","group":"c3ff9b69.9986e","order":0,"width":0,"height":0,"gtype":"gage","title":"SOC","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#b50200","#00e6d5","#8dca38"],"seg1":"20","seg2":"80","x":570,"y":320,"wires":[]},{"id":"9383e093.ab13d","type":"ui_gauge","z":"d9e4a171.78ed68","name":"","group":"c3ff9b69.9986e","order":0,"width":0,"height":0,"gtype":"gage","title":"TempInt","label":"%","format":"{{value}}","min":0,"max":"40","colors":["#b50200","#00e6d5","#8dca38"],"seg1":"20","seg2":"80","x":560,"y":360,"wires":[]},{"id":"fd4f5fe4.2a054","type":"ui_gauge","z":"d9e4a171.78ed68","name":"","group":"c3ff9b69.9986e","order":0,"width":0,"height":0,"gtype":"gage","title":"Tempext","label":"%","format":"{{value}}","min":0,"max":"40","colors":["#b50200","#00e6d5","#8dca38"],"seg1":"20","seg2":"80","x":540,"y":420,"wires":[]},{"id":"59a61781.6bb818","type":"mqtt-broker","name":"","broker":"192.168.1.79","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"c3ff9b69.9986e","type":"ui_group","name":"DefaultSBMS","tab":"fd4b8c75.b67c2","order":3,"disp":true,"width":"6","collapse":false},{"id":"fd4b8c75.b67c2","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

And here is an example of my json object

/sbms {"time":{"year":0,"month":1,"day":1,"hour":0,"minute":40,"second":53},"soc":49,"cellsMV":[3298,3283,3298,3300,3301,3298,3297,3299],"tempInt":22.8,"tempExt":21.1,"currentMA":{"battery":-1018,"pv1":503,"pv2":344,"extLoad":0},"ad2":0,"ad3":0,"ad4":0,"heat1":0,"heat2":0,"flags":{"OV":false,"OVLK":false,"UV":false,"UVLK":false,"IOT":false,"COC":false,"DOC":false,"DSC":false,"CELF":false,"OPEN":false,"LVC":false,"ECCF":false,"CFET":true,"EOC":false,"DFET":true}}

"json object" is a misnomer. JSON stands for "JavaScript Object Notation". Essentially, JSON is a string representation of a JavaScript object.

In node-red / JavaScript, if you have an object, you simply access a property by name e.g. msg.payload.time or msg.payload.cellsMV or

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.

https://nodered.org/docs/user-guide/messages

After reading that ↑ you will know to hover the debug object, click the copy path button and paste it into the place you want to use it - e.g...
image

PS, it does look like you could do with a bit learning to get the most out of node red. I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

1 Like

Once you have the JS object, you only need to access the element you require.
In the example below i inject your object and access the elements required where needed.

[{"id":"21bca0f3.b86788","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"time\":{\"year\":0,\"month\":1,\"day\":1,\"hour\":0,\"minute\":40,\"second\":53},\"soc\":49,\"cellsMV\":[3298,3283,3298,3300,3301,3298,3297,3299],\"tempInt\":22.8,\"tempExt\":21.1,\"currentMA\":{\"battery\":-1018,\"pv1\":503,\"pv2\":344,\"extLoad\":0},\"ad2\":0,\"ad3\":0,\"ad4\":0,\"heat1\":0,\"heat2\":0,\"flags\":{\"OV\":false,\"OVLK\":false,\"UV\":false,\"UVLK\":false,\"IOT\":false,\"COC\":false,\"DOC\":false,\"DSC\":false,\"CELF\":false,\"OPEN\":false,\"LVC\":false,\"ECCF\":false,\"CFET\":true,\"EOC\":false,\"DFET\":true}}","payloadType":"json","x":130,"y":2640,"wires":[["193583a.a81f4fc","c1c907c1.fef1e8","2b450846.b65208"]]},{"id":"193583a.a81f4fc","type":"ui_gauge","z":"8d22ae29.7df6d","name":"","group":"b1d45b5a.83ec7","order":0,"width":0,"height":0,"gtype":"gage","title":"SOC","label":"%","format":"{{payload.soc}}","min":0,"max":"100","colors":["#b50200","#00e6d5","#8dca38"],"seg1":"20","seg2":"80","x":310,"y":2640,"wires":[]},{"id":"c1c907c1.fef1e8","type":"ui_gauge","z":"8d22ae29.7df6d","name":"","group":"b1d45b5a.83ec7","order":0,"width":0,"height":0,"gtype":"gage","title":"TempInt","label":"%","format":"{{payload.tempInt}}","min":0,"max":"40","colors":["#b50200","#00e6d5","#8dca38"],"seg1":"20","seg2":"80","x":320,"y":2680,"wires":[]},{"id":"2b450846.b65208","type":"ui_gauge","z":"8d22ae29.7df6d","name":"","group":"b1d45b5a.83ec7","order":0,"width":0,"height":0,"gtype":"gage","title":"Tempext","label":"%","format":"{{payload.tempExt}}","min":0,"max":"40","colors":["#b50200","#00e6d5","#8dca38"],"seg1":"20","seg2":"80","x":320,"y":2720,"wires":[]},{"id":"b1d45b5a.83ec7","type":"ui_group","name":"DefaultSBMS","tab":"6310834e.f9ff6c","order":3,"disp":true,"width":"6","collapse":false},{"id":"6310834e.f9ff6c","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

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