Convert string to a format suitable for table

Hello,

I would like to display the table with the table-node in dashboard 2. I have a string as an input. It looks like:

Device1:battery low;Device2:communication error;Device3:temperature error

How can I automatically convert the string into a format suitable for tables?

It should look like:

[
    {
        "Device": "Device1",
        "Message": "battery low"
    },
    {
        "Device": "Device2",
        "Message": "communication error"
    },
    {
        "Device": "Device3",
        "Message": "temperature error"
    }
]

Best regards

What makes that string? Is it something you can control (so that it generates a valid JSON or CSV etc?)

That string comes out of my smart home system. I don't think that I can control it.

OK, if you cannot control or change the input data, then you could use a function node to split it up (there are other ways like JSONata) but here is an example in a function:

const inputString = msg.payload
const deviceMessages = inputString.split(';')

const result = deviceMessages.map(deviceMessage => {
    const [device, message] = deviceMessage.split(':')
    return {
        Device: device.trim(),
        Message: message.trim()
    }
})

msg.payload = result
return msg

NOTE: I have not taken care of any edge cases or error handling!

1 Like

Thank you very much. The script works exactly as I need it to.