Parsing data json dari Antares di node-red


Disini saya coba simulasikan data dari json antares sehingga bisa diambil datanya silahkan dimodifikasi sesuai kebutuhan

[{"id":"17560ba0.3d9064","type":"inject","z":"faf988ef.fe8c48","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"m2m:rsp\":{\"rsc\":2000,\"rqi\":\"123456\",\"pc\":{\"m2m:cin\":{\"rn\":\"cin_iuMU7IWiSbe_LIho\",\"ty\":4,\"ri\":\"/antares-cse/cin-iuMU7IWiSbe_LIho\",\"pi\":\"/antares-cse/cnt-e_WnyypcTMeYnEsT\",\"ct\":\"20210611T232216\",\"lt\":\"20210611T232216\",\"st\":0,\"cnf\":\"text/plain:0\",\"cs\":126,\"con\":\"{\\"temperature\\":28,\\"humidity\\":75,\\"wind_speed\\":6.606607,\\"rain_level\\":2.002861,\\"latitude\\":\\"-6.8718189\\",\\"longitude\\":\\"107.5872477\\"}\"}},\"to\":\"70ff8aac6a790a02:8a7288cb1b7b1a5c\",\"fr\":\"/antares-cse\"}}","payloadType":"json","x":150,"y":140,"wires":[["b6b6b140.2ad0a","d21e6167.941c2"]]},{"id":"3e106da8.411f92","type":"debug","z":"faf988ef.fe8c48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":140,"wires":[]},{"id":"b6b6b140.2ad0a","type":"function","z":"faf988ef.fe8c48","name":"","func":"var obj = msg.payload[\"m2m:rsp\"][\"pc\"][\"m2m:cin\"][\"con\"];\n\nmsg.payload = obj;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":140,"wires":[["c86838ce.7d5578"]]},{"id":"d21e6167.941c2","type":"debug","z":"faf988ef.fe8c48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":220,"wires":[]},{"id":"c86838ce.7d5578","type":"json","z":"faf988ef.fe8c48","name":"","property":"payload","action":"","pretty":false,"x":610,"y":140,"wires":[["3e106da8.411f92"]]}]

Advertisement

Parsing value json di node-red


Kali ini saya akan share bagaimana caranya parsing data json (dari mqtt) di node-red untuk mengambil valuenya saja yang nantinya data tersebut bebas kita gunakan untuk mentrigger sesuatu atau hanya untuk ditampilkan di ui node-red. Setelah browsing sana sini, tanya sana sini akhirnya entah kenapa tiba tiba malah bisa memparsing data dari mqtt yang bentuknya json, simplenya silahkan copas aja langsung flow node-red dibawah ini

[{"id":"cf8a386d.c705e8","type":"inject","z":"527576f4.5e79c8","name":"","topic":"lora","payload":"{\"temperature\":26,\"humidity\":30}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":900,"wires":[["5979b9b5.eb2a48"]]},{"id":"5979b9b5.eb2a48","type":"json","z":"527576f4.5e79c8","name":"","property":"payload","action":"obj","pretty":true,"x":290,"y":900,"wires":[["28036454.a91e2c","422cfaee.d99884"]]},{"id":"28036454.a91e2c","type":"function","z":"527576f4.5e79c8","name":"data","func":"var obj = msg.payload;\nmsg.payload = obj.temperature;\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":900,"wires":[["b7bc5dc0.c6099"]]},{"id":"422cfaee.d99884","type":"function","z":"527576f4.5e79c8","name":"data","func":"var obj = msg.payload;\nmsg.payload = obj.humidity;\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":960,"wires":[["71392630.791538"]]},{"id":"b7bc5dc0.c6099","type":"debug","z":"527576f4.5e79c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":900,"wires":[]},{"id":"71392630.791538","type":"debug","z":"527576f4.5e79c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":960,"wires":[]}]

Disini saya mencontohkan untuk menginjek data berupa json kalau mau ngambil dari mqtt juga bisa cuman untuk praktisnya saja saya masukkan kedalam inject, datanya simple sebenarnya hanya berupa json seperti dibawah ini

{
"temperature": 26,
"humidity": 30
}

Kemudian data tersebut saya ubah didalam function sehingga nanti value dari temperature dan humiditynya saja yang diambil untuk diolah lebih lanjut. Demikian catatan saya kali ini semoga bermanfaat.

Happy Coding …

Salam Ngoprek …

%d bloggers like this: