Detect value in msg.payload.body


Newbie here :slight_smile:

I have a msg.payload.body that contains one long string of text, please see below part of the payload.

"deviceModel":"MT10","alertId":"645140646630744781","alertType":"Sensor change detected","alertLevel":"informational","occurredAt":"2020-11-02T12:24:17.190976Z","alertData":{"alertConfigId":645140646621093008,"triggerData":[{"conditionId":14,"trigger":{"ts":1604318679.0,"type":"temperature","nodeId":48649295879865,"sensorValue":13.11111068725586}}],"startedAlerting":true}}"

I would like to extract values from "alertType" and "sensorValue" and "startedAlerting" and send the value to MQTT. I know how to throw it to MQTT, but I am stuck around how get values out of text only from payloads.

Anyone who can help me on my quest ? :slight_smile:

Hi @CKL1976 - welcome to the forum.

You say you've shared a part of the payload - does that mean there's more to it then that?

What you've shared looks a lot like JSON, but its missing stuff at the start, so it isn't clear if you have a full valid piece of JSON or just some fragment.

Assuming its valid JSON, you can use the JSON node to parse the string to a JavaScript Object. Once you've done that, you'll be able to access the properties you want as msg.payload.body.alertType etc.

Where does the value come from? As you may be able to set it to return a object rather than a string.