Can any one help with a more elegant way of me extracting the data I need from a JSON string

Here is a another (more general purpose) solution, imo... fwiw...

Using a change node, restructure the entire payload object to retain all the data points while also converting the key to a usable timestamp -- the new msg.payload looks like this:

{
  "PWER": {
    "cid": "PWER",
    "data": 949,
    "sid": "663404",
    "units": "W",
    "age": 3,
    "timestamp": "2023-10-25T13:16:57.000Z"
  },
  "PWER_GAC": {
    "cid": "PWER_GAC",
    "data": 129,
    "sid": "811455",
    "units": "W",
    "age": 1,
    "timestamp": "2023-10-25T13:16:59.000Z"
  }
}

I prefer to use this JSONata expression:

payload {
    cid: $ ~> | $ | 
        data {
            "timestamp": $fromMillis($number($keys($)[0])),
            "data": *
        }
    |
}

(although most "normal" people would opt for plain JS ;*)

The big advantage to building one object with multiple properties is that downstream nodes (or dashboard graphs/gages) can refer to each measurement by name (or cid), or using JS function syntax like this example:

let power = payload.PWER.data;
let units = payload.PWER.units;
let dts = new Date(payload.PWER.timestamp); // coerce string to a Date object
if (power > 500) {
    node.log(`High Power reading: ${power} ${units} on ${dts.toLocaleString()}`);
}
2 Likes