Get data from an array of objects

I'm using Climacell to obtain forecast data which I want to plot in a chart, and despite many attempts over several days, I cannot get the data into the correct format :rage:

The api returns an array of about 73 objects taking this format;

[
    {
        "lat": 53.473035,
        "lon": -1.054869,
        "precipitation": {
            "value": 20,
            "units": "mm/hr"
        },
        "observation_time": {
            "value": "2020-07-06T18:40:21.601Z"
        }
    },
    {
        "lat": 53.473035,
        "lon": -1.054869,
        "precipitation": {
            "value": 30,
            "units": "mm/hr"
        },
        "observation_time": {
            "value": "2020-07-06T18:45:21.601Z"
        }
    }
Plus 71 other objects
]

and I want to extract the precipitation.value and the corresponding observation_time.value (as a unix timestamp), in a format like this;

[
    {
        "x": 1594060821601,
        "y": 20
    },
    {
        "x": 1594061121601,
        "y": 30
    }
etc, etc
]

I can convert the UTC timestamp to a Unix timestamp by using Date.parse("2020-07-06T18:45:21.601Z") but definitely failing to get the values out in that format.

Any help would be appreciated.

I've attached an inject node loaded with the array of objects if it helps.

[{"id":"7a5026f6.990af8","type":"inject","z":"a444a9ff.e7a408","name":"Array of objects","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T19:37:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T19:42:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T19:47:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T19:52:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T19:57:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:02:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:07:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:12:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:17:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:22:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:27:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:32:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:37:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:42:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:47:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:52:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:57:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:02:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:07:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:12:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:17:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:22:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:27:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:32:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:37:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:42:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:47:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:52:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:57:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:02:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:07:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:12:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:17:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:22:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:27:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:32:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:37:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:42:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:47:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:52:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:57:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:02:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:07:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:12:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:17:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:22:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:27:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:32:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:37:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:42:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:47:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:52:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:57:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:02:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:07:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:12:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:17:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:22:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:27:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:32:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:37:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:42:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:47:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:52:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:57:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:02:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:07:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:12:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:17:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:22:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:27:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:32:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:37:15.603Z\"}}]","payloadType":"json","x":170,"y":3060,"wires":[[]]}]

Possibly better ways to do it but heres my quick try.

[{"id":"56a94eac.b50e58","type":"function","z":"ea03ca5a.9fe438","name":"","func":"msg.origpayload = msg.payload;\nmsg.payload = [];\nfor (var i in msg.origpayload) {\n    msg.payload.push({\"x\": Date.parse(msg.origpayload[0].observation_time.value), \"y\": msg.origpayload[0].precipitation.value});\n    }\nreturn msg;","outputs":1,"noerr":0,"x":270,"y":360,"wires":[["c418a275.963478"]]}]
1 Like

Wow, that was quick!
Thank you, that indeed works.

No problem. this shorter code should work to

for (var i in msg.payload) {
    msg.payload[i] = {"x": Date.parse(msg.payload[i].observation_time.value), "y": msg.payload[i].precipitation.value};
    }
return msg;
3 Likes

and the JSONata version...

payload.{"x" : $toMillis(observation_time.value), "y" : precipitation.value}

Flow:

[{"id":"cb544ccf.1705d","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"af2f40e8.d9d86","type":"inject","z":"cb544ccf.1705d","name":"Array of objects","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T19:37:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T19:42:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T19:47:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T19:52:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T19:57:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:02:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:07:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:12:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:17:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:22:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:27:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:32:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:37:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:42:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:47:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:52:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T20:57:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:02:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:07:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:12:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:17:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:22:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:27:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:32:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:37:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:42:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:47:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:52:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T21:57:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:02:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:07:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:12:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:17:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:22:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:27:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:32:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:37:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:42:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:47:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:52:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T22:57:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:02:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:07:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:12:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:17:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:22:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:27:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:32:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:37:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:42:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:47:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:52:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-06T23:57:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:02:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:07:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:12:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:17:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:22:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:27:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:32:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:37:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:42:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:47:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:52:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T00:57:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:02:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:07:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:12:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:17:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:22:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:27:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:32:15.603Z\"}},{\"lat\":53.48034,\"lon\":-1.0105372,\"precipitation\":{\"value\":0,\"units\":\"mm/hr\"},\"observation_time\":{\"value\":\"2020-07-07T01:37:15.603Z\"}}]","payloadType":"json","x":320,"y":240,"wires":[["5a2ca852.e40ec8"]]},{"id":"63341049.a93b3","type":"debug","z":"cb544ccf.1705d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":240,"wires":[]},{"id":"5a2ca852.e40ec8","type":"change","z":"cb544ccf.1705d","name":"JSONata","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.{\"x\" : $toMillis(observation_time.value), \"y\" : precipitation.value}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":240,"wires":[["63341049.a93b3"]]}]
6 Likes

This solution just gets shorter!!

Both work great, and Andrei's version just shows how powerful jsonata it.
However, as I need to format it further to use it in a chart, using the function node may be easier (especially as I can understand how it works!).

Thank you both.

2 Likes

I am still learning JSONata to, so i also appreciate @Andrei example to.
I think it works similar to my function, but the syntax is far shorter. Am i correct in thinking it executes that expression on every element of the array?

Yes, you are. One can think of the dot operator in JSONata as a kind of for loop or map method.

6 Likes

And here is the result :+1:

rain_chart3

2 Likes

Not wishing to sound greedy, but...
It would be good to also add the temperature to the chart, so I've changed the function node a little, but run into difficulties;

let rain = msg.payload;
let temp = msg.payload;

//Extract rain data from API & convert timestamp to Unix
for (var i in rain) {
    rain[i] = {"x": Date.parse(rain[i].observation_time.value), "y": (rain[i].precipitation.value).toFixed(1)};
    }
    
//Extract temperature data from API & convert timestamp to Unix
for (var t in temp) {
    temp[t] = {"x": Date.parse(temp[t].observation_time.value), "y": (temp[t].temp.value).toFixed(1)};
    }

The code runs if I comment out either of the loops, but both loops will not run together, and I get a TypeError: Cannot read property 'value' of undefined error.

I'm guessing it's something to do with the way I've defined rain & temp??

I think the vars reference each other you will need to hard clone the vars
try

var rain = JSON.parse(JSON.stringify(msg.payload));

can iget a copy of the new JSON to inject.

Yes, good old JavaScript pass by reference. So rain and temp point to the same object.. So the first loop modifies it. Then the second tries to change the same one again. You need to clone it instead if you want to start both from the same place.

Reading up on cloning... 'Writing Functions' document suggest using RED.util.cloneMessage(..) but I can't see an example of how that should be structured.

The document seems to assume a certain level of knowledge (which I don't have!)

Or just concat the array

var rain = [].concat(msg.payload);
1 Like

Yes, that works OK, I can display both precipitation & temperature now, and choose the optimum time to go running! (if Climacell are as good as they say...)

Thank you

just checked to see speed differene
array size 1000000
.concat(arr); 220 ms
RED.util.cloneMessage(arr); 4.2 seconds

1 Like

And if I recall correctly, you don't even need to convert that GMT datetime string to millis -- the ui_chart node will accept the string as-is...

2 Likes

You do not need to clone the msg.

let rain = [];
let temp = [];
//Extract rain data from API & convert timestamp to Unix
for (var i in msg.payload) {
    rain[i] = {"x": Date.parse(msg.payload[i].observation_time.value), "y": (msg.payload[i].precipitation.value).toFixed(1)};
    temp[i] = {"x": Date.parse(msg.payload[i].observation_time.value), "y": (msg.payload[i].temp.value).toFixed(1)};
}
1 Like

As usual, correct Steve :+1:

That is a much better option. Thank you.

Just to finalise, this is the contents of the function node, which feeds directly into a chart node;

let rain = [];
let temp = [];
//Extract data from API
for (var i in msg.payload) {
    rain[i] = {"x": msg.payload[i].observation_time.value, "y": (msg.payload[i].precipitation.value).toFixed(1)};
    temp[i] = {"x": msg.payload[i].observation_time.value, "y": (msg.payload[i].temp.value).toFixed(1)};
}
    
//Build chart Structure
msg.payload=[
    {
     "series":["Rain","Temp"],
     "data":[rain,temp],
     "labels":[""]
    }
            ];
     
return msg;

I just love the new 'grouping' feature in node-RED v1.1.0!

forecast

3 Likes