Change format of an array

I need to change the format of an array coming from one node to fit into another node. And I only need two of the objects in the array.

The array I need to change looks like this.

[
{"timestamp":"2023-02-25T23:00:00.000Z","price":356.8,"currency":"SEK","area":"SE4"},
{"timestamp":"2023-02-26T00:00:00.000Z","price":370.04,"currency":"SEK","area":"SE4"},
{"timestamp":"2023-02-26T01:00:00.000Z","price":387.15,"currency":"SEK","area":"SE4"},
{"timestamp":"2023-02-26T02:00:00.000Z","price":391.67,"currency":"SEK","area":"SE4"},
//..
]

and I need to change it to this

{
  "today": [
    { "value": 1, "start": "2021-06-21T00:00:00+02:00" },
    { "value": 2, "start": "2021-06-21T01:00:00+02:00" }
    //...
  ]
}

The first array is coming from nordpool-plus-api node and the second is the format needed for ps-receive-price.

Is there a way to change format of an array?

Best regards,
Joel

In a change node
set msg. payload
to JSONata J:

{
  "today": [
    $$.payload[[1..$count($$.payload)]].{ "value": $.price, "start": $.timestamp }
  ]
}

Assuming value is price, and the first element is always the previous day.

Or if value is the index

{
  "today": [
    $$.payload#$i.{ "value": $i, "start": timestamp }[[1..$count($$.payload)]]
  ]
}

Or low code version

[{"id":"3ad52acd055ad880","type":"inject","z":"65617ffeb779f51c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"timestamp\":\"2023-02-25T23:00:00.000Z\",\"price\":356.8,\"currency\":\"SEK\",\"area\":\"SE4\"},{\"timestamp\":\"2023-02-26T00:00:00.000Z\",\"price\":370.04,\"currency\":\"SEK\",\"area\":\"SE4\"},{\"timestamp\":\"2023-02-26T01:00:00.000Z\",\"price\":387.15,\"currency\":\"SEK\",\"area\":\"SE4\"},{\"timestamp\":\"2023-02-26T02:00:00.000Z\",\"price\":391.67,\"currency\":\"SEK\",\"area\":\"SE4\"}]","payloadType":"json","x":130,"y":880,"wires":[["971e2dba4ee00636"]]},{"id":"971e2dba4ee00636","type":"split","z":"65617ffeb779f51c","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":250,"y":880,"wires":[["ba926d9c6ca51768"]]},{"id":"ba926d9c6ca51768","type":"template","z":"65617ffeb779f51c","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\"start\":\"{{payload.timestamp}}\",\"value\":{{parts.index}}}","output":"json","x":400,"y":880,"wires":[["f0e8bd54dbcc3781"]]},{"id":"f0e8bd54dbcc3781","type":"join","z":"65617ffeb779f51c","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":550,"y":880,"wires":[["86efa7abf8458994"]]},{"id":"86efa7abf8458994","type":"change","z":"65617ffeb779f51c","name":"","rules":[{"t":"delete","p":"payload[0]","pt":"msg"},{"t":"move","p":"payload","pt":"msg","to":"payload.today","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":880,"wires":[["c1b0021ca796317f"]]},{"id":"c1b0021ca796317f","type":"debug","z":"65617ffeb779f51c","name":"Payload Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":626,"y":688,"wires":[]}]

I'm in awe of @E1cid's jsonata expressions but I still find them harder to understand than a function with javascript.

So here is an alternative approach using a function.
I did not attempt to strip off the first array element though it appears to belong to "yesterday" not "today"
I also made some assumptions:

  • You do want msg.payload.today[0].value to equal 1 (not 0, nor msg.payload[0].price)
  • Unlike your two examples, the date should not change from 2023 to 2021
  • You don't really need the date strings to contain "T" but omit "Z"
let newmsg = { payload: {"today":[ ] }}

for (let i=0; i< msg.payload.length; i++) {
    const thisone = { "value": i + 1, "start": msg.payload[i].timestamp  }
    newmsg.payload.today.push(thisone)
}

return newmsg;

Thanks @E1cid
That did the trick and shows me I need to put some more effort in learning more JSONata expressions.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.