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

**URL:** <https://discourse.nodered.org/t/can-any-one-help-with-a-more-elegant-way-of-me-extracting-the-data-i-need-from-a-json-string/82335>\
**Category:** General\
**Created:** [25 October 2023 12:53 UTC](https://discourse.nodered.org/t/can-any-one-help-with-a-more-elegant-way-of-me-extracting-the-data-i-need-from-a-json-string/82335 "2023-10-25T12:53:27Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [6 November 2023 02:52 UTC](https://discourse.nodered.org/t/can-any-one-help-with-a-more-elegant-way-of-me-extracting-the-data-i-need-from-a-json-string/82335/11 "2023-11-06T02:52:27Z")

</div>

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:

```auto
{
  "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:

```auto
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()}`);
}

```

---

_[View the full topic](https://discourse.nodered.org/t/can-any-one-help-with-a-more-elegant-way-of-me-extracting-the-data-i-need-from-a-json-string/82335)._
