# Change format of an array

**URL:** <https://discourse.nodered.org/t/change-format-of-an-array/75765>\
**Category:** General\
**Created:** [26 February 2023 15:46 UTC](https://discourse.nodered.org/t/change-format-of-an-array/75765 "2023-02-26T15:46:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![choel83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/choel83/32/76806_2.png) [@choel83](https://discourse.nodered.org/u/choel83)\
**Post date:** [26 February 2023 15:46 UTC](https://discourse.nodered.org/t/change-format-of-an-array/75765/1 "2023-02-26T15:46:20Z")

</div>

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.

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

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 February 2023 15:55 UTC](https://discourse.nodered.org/t/change-format-of-an-array/75765/2 "2023-02-26T15:55:59Z")

</div>

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

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

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

```

Or low code version

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

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 February 2023 16:34 UTC](https://discourse.nodered.org/t/change-format-of-an-array/75765/3 "2023-02-26T16:34:17Z")

</div>

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"

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![choel83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/choel83/32/76806_2.png) [@choel83](https://discourse.nodered.org/u/choel83)\
**Post date:** [26 February 2023 17:30 UTC](https://discourse.nodered.org/t/change-format-of-an-array/75765/4 "2023-02-26T17:30:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 March 2023 17:30 UTC](https://discourse.nodered.org/t/change-format-of-an-array/75765/5 "2023-03-12T17:30:47Z")

</div>

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