Extract values from a parsed json object using a function node or split & switch node

4,664 views
Skip to first unread message

Dany Hovard

unread,
Feb 3, 2018, 8:49:26 AM2/3/18
to Node-RED
Hello.
Was trying  to extract values from a parsed json object using a function node or split & switch node, tried to many ways and nothing seems to work fro me.
the example below I would like to have  5 outputs for the text  and numeric values.
here is my payload in json:
{
    "applicationID": "1",
    "applicationName": "test_ds18b20",
    "deviceName": "arduino_uno",
    "devEUI": "1234567890123456",
    "rxInfo": [
        {
            "mac": "aa755a0048050130",
            "rssi": -57,
            "loRaSNR": 10,
            "name": "raspberry_pi",
            "latitude": 1.466860686785175,
            "longitude": 2.019478797912605,
            "altitude": 0
        }
    ],
    "txInfo": {
        "frequency": 868100000,
        "dataRate": {
            "modulation": "LORA",
            "bandwidth": 125,
            "spreadFactor": 7
        },
        "adr": true,
        "codeRate": "4/5"
    },
    "fCnt": 9,
    "fPort": 1,
    "data": "Z29vZGJ5ZQ==",
    "object": {}
}

first i try with function node to extract "data", but it returns array like this: 
0: ""data":"Z29vZGJ5ZQ==""
1: "Z29vZGJ5ZQ=="

i dont need array i need string
function:
var regexsearch = /\"data\":\"(.*?)\"/i;
var my = msg.payload.match(regexsearch);
msg.payload = my;
return msg;

but i need to get only this Z29vZGJ5ZQ==

than i try with split & switch nodes and gets the whole linelike this: ""data":"Z29vZGJ5ZQ==""
but i need to get only this Z29vZGJ5ZQ==


and here is my flow:

[{"id":"d46d38e2.27cc78","type":"inject","z":"ff592a31.cf21a8","name":"","topic":"","payload":"{\"applicationID\":\"1\",\"applicationName\":\"test_ds18b20\",\"deviceName\":\"arduino_uno\",\"devEUI\":\"1234567890123456\",\"rxInfo\":[{\"mac\":\"aa755a0048050130\",\"rssi\":-57,\"loRaSNR\":10,\"name\":\"raspberry_pi\",\"latitude\":48.466860686785175,\"longitude\":35.019478797912605,\"altitude\":0}],\"txInfo\":{\"frequency\":868100000,\"dataRate\":{\"modulation\":\"LORA\",\"bandwidth\":125,\"spreadFactor\":7},\"adr\":true,\"codeRate\":\"4/5\"},\"fCnt\":9,\"fPort\":1,\"data\":\"Z29vZGJ5ZQ==\",\"object\":{}}","payloadType":"json","repeat":"","crontab":"","once":false,"x":90,"y":160,"wires":[["1a34819e.743eee"]]},{"id":"105db6d9.0df1c9","type":"debug","z":"ff592a31.cf21a8","name":"","active":true,"console":"false","complete":"false","x":610,"y":100,"wires":[]},{"id":"1ac8a3e1.8f379c","type":"split","z":"ff592a31.cf21a8","name":"","splt":",","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"","x":250,"y":340,"wires":[["c10ec515.102d38"]]},{"id":"c10ec515.102d38","type":"switch","z":"ff592a31.cf21a8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"\"data\":","vt":"str"},{"t":"cont","v":"\"latitude\":","vt":"str"}],"checkall":"true","outputs":2,"x":370,"y":340,"wires":[["105db6d9.0df1c9"],["6b2d5d19.7868e4"]]},{"id":"1a34819e.743eee","type":"json","z":"ff592a31.cf21a8","name":"","pretty":false,"x":115.55555555555556,"y":312.22222222222223,"wires":[["1ac8a3e1.8f379c","bae9fa5d.a9f238"]]},{"id":"bae9fa5d.a9f238","type":"function","z":"ff592a31.cf21a8","name":"match","func":"var regexsearch = /\\\"data\\\":\\\"(.*?)\\\"/i;\nvar my = msg.payload.match(regexsearch);\nmsg.payload = my;\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":160,"wires":[["105db6d9.0df1c9"]]},{"id":"6b2d5d19.7868e4","type":"debug","z":"ff592a31.cf21a8","name":"","active":true,"console":"false","complete":"false","x":610,"y":180,"wires":[]}]

Thanks for help


Nick O'Leary

unread,
Feb 3, 2018, 8:56:44 AM2/3/18
to Node-RED Mailing List
Hi,

we've recently added some documentation on working with message properties - https://nodered.org/docs/user-guide/messages

To access the properties inside a JSON string you first need to parse it to a JavaScript object. You can do that with the JSON node.

Once you have it as an object, you can address its contents directly. For example, msg.payload.data will give you what you want.

Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/418a192f-de81-46a6-922a-8c11a9afd58c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Dany Hovard

unread,
Feb 3, 2018, 9:12:33 AM2/3/18
to Node-RED
Thanks Nik i wiil try this.
But can you help me if i cant figure this?

суббота, 3 февраля 2018 г., 15:56:44 UTC+2 пользователь Nick O'Leary написал:
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

AIOT MAKER

unread,
Feb 3, 2018, 9:18:28 AM2/3/18
to Node-RED
One possible solution...using the change node.


[{"id":"6fedc63f.325e48","type":"inject","z":"f4c4b9a6.20cd68","name":"","topic":"","payload":"{\"applicationID\":\"1\",\"applicationName\":\"test_ds18b20\",\"deviceName\":\"arduino_uno\",\"devEUI\":\"1234567890123456\",\"rxInfo\":[{\"mac\":\"aa755a0048050130\",\"rssi\":-57,\"loRaSNR\":10,\"name\":\"raspberry_pi\",\"latitude\":48.466860686785175,\"longitude\":35.019478797912605,\"altitude\":0}],\"txInfo\":{\"frequency\":868100000,\"dataRate\":{\"modulation\":\"LORA\",\"bandwidth\":125,\"spreadFactor\":7},\"adr\":true,\"codeRate\":\"4/5\"},\"fCnt\":9,\"fPort\":1,\"data\":\"Z29vZGJ5ZQ==\",\"object\":{}}","payloadType":"json","repeat":"","crontab":"","once":false,"x":210,"y":60,"wires":[["6c467282.74cd5c"]]},{"id":"b992f57b.7ce3d8","type":"debug","z":"f4c4b9a6.20cd68","name":"","active":true,"console":"false","complete":"true","x":890,"y":60,"wires":[]},{"id":"6c467282.74cd5c","type":"change","z":"f4c4b9a6.20cd68","name":"","rules":[{"t":"move","p":"payload.data","pt":"msg","to":"data","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":60,"wires":[["118a84d0.631a0b"]]},{"id":"118a84d0.631a0b","type":"switch","z":"f4c4b9a6.20cd68","name":"","property":"data","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","outputs":1,"x":630,"y":60,"wires":[["b992f57b.7ce3d8"]]}]

Julian Knight

unread,
Feb 3, 2018, 10:02:14 AM2/3/18
to Node-RED
For things like this, the best thing to do is to have a go then share what you've done so that you can ask specifics. There are lots of us who try to help but we typically have little time so answering specific questions and helping you learn is much easier for us so you are more likely to get quality help.

AIOT MAKER

unread,
Feb 3, 2018, 10:20:22 AM2/3/18
to Node-RED
Hello Dany,

pls disregard my previous answer... it simply does not work.

The one below (based on your original flow)  is far from optimal but at least it works.

[{"id":"d8577d64.426b5","type":"inject","z":"2797568e.4ecc6a","name":"","topic":"","payload":"{\"applicationID\":\"1\",\"applicationName\":\"test_ds18b20\",\"deviceName\":\"arduino_uno\",\"devEUI\":\"1234567890123456\",\"rxInfo\":[{\"mac\":\"aa755a0048050130\",\"rssi\":-57,\"loRaSNR\":10,\"name\":\"raspberry_pi\",\"latitude\":48.466860686785175,\"longitude\":35.019478797912605,\"altitude\":0}],\"txInfo\":{\"frequency\":868100000,\"dataRate\":{\"modulation\":\"LORA\",\"bandwidth\":125,\"spreadFactor\":7},\"adr\":true,\"codeRate\":\"4/5\"},\"fCnt\":9,\"fPort\":1,\"data\":\"Z29vZGJ5ZQ==\",\"object\":{}}","payloadType":"str","repeat":"","crontab":"","once":false,"x":150,"y":240,"wires":[["9e6941e4.0387a"]]},{"id":"86778feb.237d1","type":"split","z":"2797568e.4ecc6a","name":"","splt":",","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"topic","x":430,"y":240,"wires":[["2a21ac73.c1cf64"]]},{"id":"9e6941e4.0387a","type":"json","z":"2797568e.4ecc6a","name":"","pretty":false,"x":290,"y":240,"wires":[["86778feb.237d1"]]},{"id":"2a21ac73.c1cf64","type":"switch","z":"2797568e.4ecc6a","name":"","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"data","vt":"str"},{"t":"cont","v":"rxInfo","vt":"str"}],"checkall":"true","outputs":2,"x":610,"y":280,"wires":[["2c084b.14b617b6"],["92e39cee.a5b59"]]},{"id":"2c084b.14b617b6","type":"debug","z":"2797568e.4ecc6a","name":"data","active":true,"console":"false","complete":"true","x":790,"y":200,"wires":[]},{"id":"8667e7d2.727958","type":"debug","z":"2797568e.4ecc6a","name":"latitude","active":true,"console":"false","complete":"true","x":940,"y":280,"wires":[]},{"id":"92e39cee.a5b59","type":"change","z":"2797568e.4ecc6a","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[0].latitude","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"latitude","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":280,"wires":[["8667e7d2.727958"]]}]

AIOT MAKER

unread,
Feb 3, 2018, 10:27:42 AM2/3/18
to Node-RED
I like most this solution that uses a function node...


[{"id":"34010f8d.2150d","type":"inject","z":"2797568e.4ecc6a","name":"","topic":"","payload":"{\"applicationID\":\"1\",\"applicationName\":\"test_ds18b20\",\"deviceName\":\"arduino_uno\",\"devEUI\":\"1234567890123456\",\"rxInfo\":[{\"mac\":\"aa755a0048050130\",\"rssi\":-57,\"loRaSNR\":10,\"name\":\"raspberry_pi\",\"latitude\":48.466860686785175,\"longitude\":35.019478797912605,\"altitude\":0}],\"txInfo\":{\"frequency\":868100000,\"dataRate\":{\"modulation\":\"LORA\",\"bandwidth\":125,\"spreadFactor\":7},\"adr\":true,\"codeRate\":\"4/5\"},\"fCnt\":9,\"fPort\":1,\"data\":\"Z29vZGJ5ZQ==\",\"object\":{}}","payloadType":"str","repeat":"","crontab":"","once":false,"x":250,"y":460,"wires":[["3439ee94.fea142"]]},{"id":"3439ee94.fea142","type":"json","z":"2797568e.4ecc6a","name":"","pretty":false,"x":390,"y":460,"wires":[["e4667a09.808e98"]]},{"id":"e4667a09.808e98","type":"function","z":"2797568e.4ecc6a","name":"Parse","func":"msg1 = {\"topic\":\"latitude\", \"payload\":msg.payload.rxInfo[0].latitude};\nmsg2 = {\"topic\":\"data\", \"payload\":msg.payload.data};\nreturn [msg1, msg2];","outputs":"2","noerr":0,"x":550.1111518012153,"y":458.88888888888886,"wires":[["f18090cc.c555c"],["89bfc2e4.410e6"]]},{"id":"f18090cc.c555c","type":"debug","z":"2797568e.4ecc6a","name":"data","active":true,"console":"false","complete":"true","x":750,"y":420,"wires":[]},{"id":"89bfc2e4.410e6","type":"debug","z":"2797568e.4ecc6a","name":"latitude","active":true,"console":"false","complete":"true","x":760,"y":480,"wires":[]}]


AIOT MAKER

unread,
Feb 3, 2018, 11:02:21 AM2/3/18
to Node-RED
Perhaps a 3rd way...that generates a different data output.  This is why I love node-RED... so many ways to do things...







[{"id":"69d60a10.6e2394","type":"inject","z":"a7e31164.da3e9","name":"","topic":"","payload":"{\"applicationID\":\"1\",\"applicationName\":\"test_ds18b20\",\"deviceName\":\"arduino_uno\",\"devEUI\":\"1234567890123456\",\"rxInfo\":[{\"mac\":\"aa755a0048050130\",\"rssi\":-57,\"loRaSNR\":10,\"name\":\"raspberry_pi\",\"latitude\":48.466860686785175,\"longitude\":35.019478797912605,\"altitude\":0}],\"txInfo\":{\"frequency\":868100000,\"dataRate\":{\"modulation\":\"LORA\",\"bandwidth\":125,\"spreadFactor\":7},\"adr\":true,\"codeRate\":\"4/5\"},\"fCnt\":9,\"fPort\":1,\"data\":\"Z29vZGJ5ZQ==\",\"object\":{}}","payloadType":"str","repeat":"","crontab":"","once":false,"x":330,"y":400,"wires":[["380dfccd.965564"]]},{"id":"380dfccd.965564","type":"json","z":"a7e31164.da3e9","name":"","pretty":false,"x":470,"y":400,"wires":[["1781295f.7607d7"]]},{"id":"f8077684.6ff8e8","type":"debug","z":"a7e31164.da3e9","name":"latitude-longitude-data","active":true,"console":"false","complete":"true","x":840,"y":400,"wires":[]},{"id":"1781295f.7607d7","type":"change","z":"a7e31164.da3e9","name":"","rules":[{"t":"set","p":"latitude","pt":"msg","to":"payload.rxInfo[0].latitude","tot":"msg"},{"t":"set","p":"longitude","pt":"msg","to":"payload.rxInfo[0].longitude","tot":"msg"},{"t":"set","p":"data","pt":"msg","to":"payload.data","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":400,"wires":[["f8077684.6ff8e8"]]}]

Dany Hovard

unread,
Feb 4, 2018, 9:32:43 AM2/4/18
to Node-RED
Thanks guys, I really appreciate it!!
Reply all
Reply to author
Forward
0 new messages