Changing structure of msg.payload from multidimensional array

[["TT105"," 59"],["TT106"," 59"],["TT201"," 59"],["TT202"," 59"],["TT301"," 59"],["TT302"," 59"],["TT303"," 59"],["TT304"," 59"],["TT001"," 65"],["TT104"," 65"]]

This is my msg.payload - I want to redo the structure of the payload to match the structure of the following payload:


Any immediate thoughts on how I should approach this? :slight_smile:


I have remade the structure so I now get one payload per element;
payload = "TT104: 32"
I now want to make it so that my payload is a multidimensional array,:
Msg.payload[0][0], where msg.payload[0] = "TT104" and msg.payload[0][0] = 32 (numeric), any ideas?

Setting payload[0] to "tt104", means you can not set payload[0][0] as the payload[0] is now a string, so [0][0] can not exist.

I think you need to explain more clearly what you require, and may be what you wish to do/achieve.

Ah I understand.

I want to convert my payload from the string structure "TT104: 32" to a JSON object type:
So I can seperate the tag name from the value when parsed to an Influx database:)

show us a debug of incoming string, and explain where it comes form.

{"topic":"MQTT","payload":"TT001: 56/TT104: 56/TT105: 56/TT106: 56/TT201: 56/TT202: 56/TT301: 56/TT302: 56/TT303: 56/TT304: 56/@MYSITE","qos":1,"retain":false,"_msgid":"467147b9.1b7d28"}

This is the output from my MQTT node:
I managed to seperate @MYSITE and parse it as
msg.measurement : "MYSITE"

Preferrably I would really like to maintain all the variables in one payload and redo the payload:
payload":"TT001: 56/TT104: 56/TT105: 56/TT106: 56/TT201: 56/TT202: 56/TT301: 56/TT302: 56/TT303: 56/TT304: 56/"

To obtain the following structure:

This looks more like a question of how JS works, maybe you should try to ask those on another forum for a better answer.

In the meantime, I would do something like

const input = [["a", 1], ["b", 2]];
const output = {}
input .map(([key, value]) => { output [key]=value})
output  // { a: 1, b: 2 }

do you have control on how the mqtt payload is constructed? As it would be best to send the string in json format, then you would not need to do any thing to convert it.
If you can not change the mqtt payload, this should work

[{"id":"8b0f0b705735122d","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"TT001: 56/TT104: 56/TT105: 56/TT106: 56/TT201: 56/TT202: 56/TT301: 56/TT302: 56/TT303: 56/TT304: 66/@MYSITE","payloadType":"str","x":150,"y":3280,"wires":[["b238257.6f33858"]]},{"id":"b238257.6f33858","type":"function","z":"bf9e1e33.030598","name":"","func":"msg.payload = msg.payload.split(\"/\").slice(0,-1);\nmsg.payload = msg.payload.reduce((acc,str) =>{\n    let arr = str.split(\": \");\n    return Object.assign(\n        acc,\n        {[arr[0]]:arr[1]})\n},{})\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":3260,"wires":[["9c792ae0.1bc0b"]]},{"id":"9c792ae0.1bc0b","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":3300,"wires":[]}]
1 Like

Thank you so, so much for this excellent flow. It worked perfectly!

As you probably understand, I'm rather new to JS and I just needed to get this working asap.

I am so grateful :slight_smile: