# How to convert payload object to array of elements

**URL:** <https://discourse.nodered.org/t/how-to-convert-payload-object-to-array-of-elements/45665>\
**Category:** General\
**Created:** [14 May 2021 19:53 UTC](https://discourse.nodered.org/t/how-to-convert-payload-object-to-array-of-elements/45665 "2021-05-14T19:53:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafterkai](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rafterkai/32/41777_2.png) [@rafterkai](https://discourse.nodered.org/u/rafterkai)\
**Post date:** [14 May 2021 19:53 UTC](https://discourse.nodered.org/t/how-to-convert-payload-object-to-array-of-elements/45665/1 "2021-05-14T19:53:40Z")

</div>

I'm not sure if the title correctly dictates what i need and i'm sorry if this something simple to do but i have no experience with javascript or coding and i'm fairly new to node red.

Here's an example of what i'm trying to accomplish,  
I have the following payload:

```auto
{
   "code":[
       "352",
       "353",
       "354"
   ],
   "number":[
       447.1,
       674.14,
       493.78
   ],
   "date":[
       "31/3/21",
       "1/4/21",
       "6/4/21"
   ]
}

```

I would like to convert it to

```auto
[
   {
       "code":"352",
       "number":447.1,
       "date":"31/3/21"
   },
   {
       "code":"353",
       "number":674.14,
       "date":"1/4/21"
   },
   {
       "code":"354",
       "number":493.78,
       "date":"6/4/21"
   }

 ]

```

Is there a node that can do this?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [14 May 2021 20:27 UTC](https://discourse.nodered.org/t/how-to-convert-payload-object-to-array-of-elements/45665/2 "2021-05-14T20:27:42Z")

</div>

This might not be the most efficient but it does the trick. In a function node:

```auto
const x = {
   "code":[
       "352",
       "353",
       "354"
   ],
   "number":[
       447.1,
       674.14,
       493.78
   ],
   "date":[
       "31/3/21",
       "1/4/21",
       "6/4/21"
   ]
}

const out = []

Object.keys(x).forEach( entry => {
    const e = {}
    
    e.code = x[entry][0]
    e.number = x[entry][1]
    e.date = x[entry][2]
    
    out.push(e)
})

msg.payload = out
return msg

```

---

<div class="post-metadata">

**Author:** ![rafterkai](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rafterkai/32/41777_2.png) [@rafterkai](https://discourse.nodered.org/u/rafterkai)\
**Post date:** [14 May 2021 21:10 UTC](https://discourse.nodered.org/t/how-to-convert-payload-object-to-array-of-elements/45665/4 "2021-05-14T21:10:32Z")

</div>

Thank you,  
This is in the right direction but not exactly what i need.

the result got me:

```auto
[
   {
       "code":"352",
       "number":"353",
       "date":"354"
   },
   {
       "code":447.1,
       "number":674.14,
       "date":493.78
   },
   {
       "code":"31/3/21",
       "number":"1/4/21",
       "date":"6/4/21"
   }
]

```

The elements in the array get the values from the same original object.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [14 May 2021 21:13 UTC](https://discourse.nodered.org/t/how-to-convert-payload-object-to-array-of-elements/45665/5 "2021-05-14T21:13:06Z")

</div>

Oops, so much for trying to rush. You will need to pick it apart a bit further then. All the elements are there, just not quite in the right order! 😱

---

<div class="post-metadata">

**Author:** ![rafterkai](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rafterkai/32/41777_2.png) [@rafterkai](https://discourse.nodered.org/u/rafterkai)\
**Post date:** [14 May 2021 21:27 UTC](https://discourse.nodered.org/t/how-to-convert-payload-object-to-array-of-elements/45665/6 "2021-05-14T21:27:41Z")

</div>

After 5 hours of searching the web, i finally found a way!

this is a link to the solution in case anyone ever needs it!

> <https://stackoverflow.com/questions/42342552/best-way-to-convert-object-with-arrays-to-array-with-objects-and-viceversa>

---

<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:** [28 May 2021 21:27 UTC](https://discourse.nodered.org/t/how-to-convert-payload-object-to-array-of-elements/45665/7 "2021-05-28T21:27:48Z")

</div>

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