Extract value from payload array

HI,
I try to extract the temperature for each channel from the following payload:

{"system":{"time":"1597405052","unit":"C","rssi":-69,"online":2},"channel":[{"number":1,"name":"Kanal 1","typ":0,"temp":999.00,"min":10.00,"max":35.00,"alarm":1,"color":"#00FF00","fixed":false,"connected":false},{"number":2,"name":"Kanal 2","typ":0,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#22B14C","fixed":false,"connected":false},{"number":3,"name":"Kanal 3","typ":3,"temp":25.80,"min":10.00,"max":35.00,"alarm":2,"color":"#EF562D","fixed":false,"connected":false},{"number":4,"name":"Kanal 4","typ":0,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#FFC100","fixed":false,"connected":false},{"number":5,"name":"Kanal 5","typ":0,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#A349A4","fixed":false,"connected":false},{"number":6,"name":"Kanal 6","typ":0,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#804000","fixed":false,"connected":false},{"number":7,"name":"Kanal 7","typ":0,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#5587A2","fixed":false,"connected":false},{"number":8,"name":"Kanal 8","typ":0,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#5C7148","fixed":false,"connected":false},{"number":9,"name":"Kanal 9","typ":17,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#5C7148","fixed":true,"connected":false},{"number":10,"name":"Kanal 10","typ":17,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#5C7148","fixed":true,"connected":false}],"pitmaster":{"type":["off","manual","auto"],"pm":[{"id":0,"channel":1,"pid":1,"value":0,"set":135.00,"typ":"off","set_color":"#ff0000","value_color":"#000000"},{"id":1,"channel":2,"pid":1,"value":0,"set":50.00,"typ":"off","set_color":"#FE2EF7","value_color":"#848484"}]}}

But I failed every time. Could somebody help me to sepearate this values out?
IĀ“ve tried to use the function:

> var newMsg = { payload: msg.payload["channel"].[1].temp };
> return newMsg;

Hi,

you want to extract a single temp value ?

// if you want a single value. example at position 2
msg.payload = msg.payload.channel[2].temp;
return msg;

Flow example with other cases of extracting values
jsonata expression comes in handy if you want to filter out all the temp values

[
    {
        "id": "a8f6b829.ca2e1",
        "type": "inject",
        "z": "d0285368.3cfd98",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"system\":{\"time\":\"1597405052\",\"unit\":\"C\",\"rssi\":-69,\"online\":2},\"channel\":[{\"number\":1,\"name\":\"Kanal 1\",\"typ\":0,\"temp\":999,\"min\":10,\"max\":35,\"alarm\":1,\"color\":\"#00FF00\",\"fixed\":false,\"connected\":false},{\"number\":2,\"name\":\"Kanal 2\",\"typ\":0,\"temp\":999,\"min\":10,\"max\":35,\"alarm\":0,\"color\":\"#22B14C\",\"fixed\":false,\"connected\":false},{\"number\":3,\"name\":\"Kanal 3\",\"typ\":3,\"temp\":25.8,\"min\":10,\"max\":35,\"alarm\":2,\"color\":\"#EF562D\",\"fixed\":false,\"connected\":false},{\"number\":4,\"name\":\"Kanal 4\",\"typ\":0,\"temp\":999,\"min\":10,\"max\":35,\"alarm\":0,\"color\":\"#FFC100\",\"fixed\":false,\"connected\":false},{\"number\":5,\"name\":\"Kanal 5\",\"typ\":0,\"temp\":999,\"min\":10,\"max\":35,\"alarm\":0,\"color\":\"#A349A4\",\"fixed\":false,\"connected\":false},{\"number\":6,\"name\":\"Kanal 6\",\"typ\":0,\"temp\":999,\"min\":10,\"max\":35,\"alarm\":0,\"color\":\"#804000\",\"fixed\":false,\"connected\":false},{\"number\":7,\"name\":\"Kanal 7\",\"typ\":0,\"temp\":999,\"min\":10,\"max\":35,\"alarm\":0,\"color\":\"#5587A2\",\"fixed\":false,\"connected\":false},{\"number\":8,\"name\":\"Kanal 8\",\"typ\":0,\"temp\":999,\"min\":10,\"max\":35,\"alarm\":0,\"color\":\"#5C7148\",\"fixed\":false,\"connected\":false},{\"number\":9,\"name\":\"Kanal 9\",\"typ\":17,\"temp\":999,\"min\":10,\"max\":35,\"alarm\":0,\"color\":\"#5C7148\",\"fixed\":true,\"connected\":false},{\"number\":10,\"name\":\"Kanal 10\",\"typ\":17,\"temp\":999,\"min\":10,\"max\":35,\"alarm\":0,\"color\":\"#5C7148\",\"fixed\":true,\"connected\":false}],\"pitmaster\":{\"type\":[\"off\",\"manual\",\"auto\"],\"pm\":[{\"id\":0,\"channel\":1,\"pid\":1,\"value\":0,\"set\":135,\"typ\":\"off\",\"set_color\":\"#ff0000\",\"value_color\":\"#000000\"},{\"id\":1,\"channel\":2,\"pid\":1,\"value\":0,\"set\":50,\"typ\":\"off\",\"set_color\":\"#FE2EF7\",\"value_color\":\"#848484\"}]}}",
        "payloadType": "json",
        "x": 310,
        "y": 280,
        "wires": [
            [
                "1339e6b1.e88f01",
                "7de71d5e.e98e0c",
                "250092f2.c13b5e"
            ]
        ]
    },
    {
        "id": "1339e6b1.e88f01",
        "type": "function",
        "z": "d0285368.3cfd98",
        "name": "",
        "func": "// if you want a single value. example at position 2\nmsg.payload = msg.payload.channel[2].temp;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 580,
        "y": 280,
        "wires": [
            [
                "4614741.9c0ba0c"
            ]
        ]
    },
    {
        "id": "7de71d5e.e98e0c",
        "type": "debug",
        "z": "d0285368.3cfd98",
        "name": "original payload",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 510,
        "y": 220,
        "wires": []
    },
    {
        "id": "4614741.9c0ba0c",
        "type": "debug",
        "z": "d0285368.3cfd98",
        "name": "single value",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 860,
        "y": 280,
        "wires": []
    },
    {
        "id": "250092f2.c13b5e",
        "type": "change",
        "z": "d0285368.3cfd98",
        "name": "use of jsonata",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.channel.temp",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 590,
        "y": 380,
        "wires": [
            [
                "a40cc6c7.a224f8"
            ]
        ]
    },
    {
        "id": "a40cc6c7.a224f8",
        "type": "debug",
        "z": "d0285368.3cfd98",
        "name": "all Temp in array",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 890,
        "y": 380,
        "wires": []
    }
]
2 Likes

The quickest is to use jsonata expression in a change node:

payload.channel.{"temp":**.temp,"channel":**.number}
or if you just want an array of values: payload.channel.temp

Example flow.

[{"id":"c8be76ea.a0a0b8","type":"inject","z":"79b6dadf.7ea13c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"system\":{\"time\":\"1597405052\",\"unit\":\"C\",\"rssi\":-69,\"online\":2},\"channel\":[{\"number\":1,\"name\":\"Kanal 1\",\"typ\":0,\"temp\":999.00,\"min\":10.00,\"max\":35.00,\"alarm\":1,\"color\":\"#00FF00\",\"fixed\":false,\"connected\":false},{\"number\":2,\"name\":\"Kanal 2\",\"typ\":0,\"temp\":999.00,\"min\":10.00,\"max\":35.00,\"alarm\":0,\"color\":\"#22B14C\",\"fixed\":false,\"connected\":false},{\"number\":3,\"name\":\"Kanal 3\",\"typ\":3,\"temp\":25.80,\"min\":10.00,\"max\":35.00,\"alarm\":2,\"color\":\"#EF562D\",\"fixed\":false,\"connected\":false},{\"number\":4,\"name\":\"Kanal 4\",\"typ\":0,\"temp\":999.00,\"min\":10.00,\"max\":35.00,\"alarm\":0,\"color\":\"#FFC100\",\"fixed\":false,\"connected\":false},{\"number\":5,\"name\":\"Kanal 5\",\"typ\":0,\"temp\":999.00,\"min\":10.00,\"max\":35.00,\"alarm\":0,\"color\":\"#A349A4\",\"fixed\":false,\"connected\":false},{\"number\":6,\"name\":\"Kanal 6\",\"typ\":0,\"temp\":999.00,\"min\":10.00,\"max\":35.00,\"alarm\":0,\"color\":\"#804000\",\"fixed\":false,\"connected\":false},{\"number\":7,\"name\":\"Kanal 7\",\"typ\":0,\"temp\":999.00,\"min\":10.00,\"max\":35.00,\"alarm\":0,\"color\":\"#5587A2\",\"fixed\":false,\"connected\":false},{\"number\":8,\"name\":\"Kanal 8\",\"typ\":0,\"temp\":999.00,\"min\":10.00,\"max\":35.00,\"alarm\":0,\"color\":\"#5C7148\",\"fixed\":false,\"connected\":false},{\"number\":9,\"name\":\"Kanal 9\",\"typ\":17,\"temp\":999.00,\"min\":10.00,\"max\":35.00,\"alarm\":0,\"color\":\"#5C7148\",\"fixed\":true,\"connected\":false},{\"number\":10,\"name\":\"Kanal 10\",\"typ\":17,\"temp\":999.00,\"min\":10.00,\"max\":35.00,\"alarm\":0,\"color\":\"#5C7148\",\"fixed\":true,\"connected\":false}],\"pitmaster\":{\"type\":[\"off\",\"manual\",\"auto\"],\"pm\":[{\"id\":0,\"channel\":1,\"pid\":1,\"value\":0,\"set\":135.00,\"typ\":\"off\",\"set_color\":\"#ff0000\",\"value_color\":\"#000000\"},{\"id\":1,\"channel\":2,\"pid\":1,\"value\":0,\"set\":50.00,\"typ\":\"off\",\"set_color\":\"#FE2EF7\",\"value_color\":\"#848484\"}]}}","payloadType":"json","x":192,"y":216,"wires":[["45e400d1.0bf568"]]},{"id":"46c67b46.ff822c","type":"debug","z":"79b6dadf.7ea13c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":578,"y":216,"wires":[]},{"id":"45e400d1.0bf568","type":"change","z":"79b6dadf.7ea13c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.channel.{\"temp\":**.temp,\"channel\":**.number}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":368,"y":216,"wires":[["46c67b46.ff822c"]]}]

Output:

[{"temp":999,"channel":1},{"temp":999,"channel":2},{"temp":25.8,"channel":3},{"temp":999,"channel":4},{"temp":999,"channel":5},{"temp":999,"channel":6},{"temp":999,"channel":7},{"temp":999,"channel":8},{"temp":999,"channel":9},{"temp":999,"channel":10}]
3 Likes

Thanks for everyone!
This was the Solution and a good explanation!

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