parsing – How to read Json in Node-Red

I would like to ask for a help with reading JSON format from a device in Node-Red. I succesfully made to read the JSON content in Python, but applying the same strategy in Node-Red does not yield a success.

I tried several approches.

  1. Use JSON node, it reports

    Unexpected token in JSON at position 0

  2. I tried the function node

    var str = JSON.stringify(msg.payload);
    var data = JSON.parse(str);
    // var Temp1 = data["Monitor"]["S"]["S1"]["item1"]["value"];
    msg.payload = data["Monitor"]["S"]["S1"]["item1"]["value"];
    return msg, data;

And Node-red reports:
> TypeError: Cannot read properties of undefined (reading ‘S’)

To read the Json from the device I use HTTP get

{
“Monitor”: {
“DeviceInfo”: {
“DeviceName”: “TCW241”,
“HostName”: “TCW241”,
“ID”: “5C:32:C5:00:9E:C2”,
“FwVer”: “TCW241-v1.240”,
“MnfInfo”: “www.teracomsystems.com“,
“SysContact”: “Petr”,
“SysName”: “Garaze Relay”,
“SysLocation”: “Garaze Anglie”
},
“S”: {
“S1”: {
“description”: “Garaz”,
“id”: “28245C75D0013C29”,
“item1”: {
“value”: “9.4”,
“unit”: “°C”,
“alarm”: “0”,
“min”: “-40.0”,
“max”: “85.0”,
“hys”: “8.5”
},
“item2”: {
“value”: “—“,
“unit”: “—“,
“alarm”: “0”,
“min”: “—“,
“max”: “—“,
“hys”: “—“…

Read more here: Source link