# How to convert object into array?

**URL:** <https://discourse.nodered.org/t/how-to-convert-object-into-array/31563>\
**Category:** General\
**Created:** [14 August 2020 12:20 UTC](https://discourse.nodered.org/t/how-to-convert-object-into-array/31563 "2020-08-14T12:20:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharper](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@sharper](https://discourse.nodered.org/u/sharper)\
**Post date:** [14 August 2020 12:20 UTC](https://discourse.nodered.org/t/how-to-convert-object-into-array/31563/1 "2020-08-14T12:20:26Z")

</div>

I am having some issues understanding how to create an array from an object, This object is one piece of an array(results from a mysql query) maybe I just need more coffee:

> > > Current object looks like this:

{"task\_id":4,  
"assigned":"testuser",  
"due\_date":"2020-08-16",  
"complete\_by":"14:33",  
"details":"some details"}

> > > My goal is to have an array that looks like this:

{  
"title": "task\_id",  
"value": "4"  
},  
{  
"title": "assigned",  
"value": "testuser"  
},  
{  
"title": "due\_date",  
"value": "2020-08-16"  
},  
{  
"title": "complete\_by",  
"value": "14:33"  
},  
{  
"title": "details",  
"value": "some details"  
}

New to node red and I am not a dev, be gentle... lol

Thanks!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 August 2020 12:46 UTC](https://discourse.nodered.org/t/how-to-convert-object-into-array/31563/2 "2020-08-14T12:46:27Z")

</div>

Some more context would be useful for suggesting the best solution, so is the object in msg.payload or are you already in a Function node, interpreting the sql node result array, or something else?  
However, can you describe what you are going to do with the array, there may be a better way of solving the larger problem.

---

<div class="post-metadata">

**Author:** ![sharper](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@sharper](https://discourse.nodered.org/u/sharper)\
**Post date:** [14 August 2020 13:18 UTC](https://discourse.nodered.org/t/how-to-convert-object-into-array/31563/3 "2020-08-14T13:18:18Z")

</div>

This it the data returned from an upstream mysql query(direct output from the mysql node). are you thinking there might be a better way to format the output of the mysql query?

Here is a snip of a payload:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a6093e283145b27b6252e8ed8e91d654f00b6410.png)

Thanks!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 August 2020 13:27 UTC](https://discourse.nodered.org/t/how-to-convert-object-into-array/31563/4 "2020-08-14T13:27:20Z")

</div>

This will work as described in your first post...

add a function node and feed your database output to it...

```auto
var el0 = msg.payload[0]; //get element zero of your data
var arr = []; //a new array for populating results

for(let prop in el0) {
  let val = el0[prop];
  arr.push({
    "title": prop,
    "value": val
  });
}

msg.payload = arr; //set payload
return msg; //return the msg

```

### caveats

This only works with one element of your array (because the 2nd element would create duplicates) but if you want that then put the whole thing in a loop.

### This is a test to prove it works...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/31694cc8051ab7c204d6e73e4caa81a6cdfe7b14.png)

---

<div class="post-metadata">

**Author:** ![sharper](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@sharper](https://discourse.nodered.org/u/sharper)\
**Post date:** [14 August 2020 13:59 UTC](https://discourse.nodered.org/t/how-to-convert-object-into-array/31563/5 "2020-08-14T13:59:37Z")

</div>

That is exactly what I was looking to do! Just need to figure out the proper way to do the loop now!

Thank you very much!!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 August 2020 14:30 UTC](https://discourse.nodered.org/t/how-to-convert-object-into-array/31563/6 "2020-08-14T14:30:00Z")

</div>

Try this...

```auto
var outer = msg.payload; //get your data
var arr = []; //a new array for populating results

for(let i = 0; i < outer.length; i++) {

let el = outer[i];
for(let prop in el) {
  let val = el[prop];
  arr.push({
    "title": prop,
    "value": val
  });
}

}
msg.payload = arr; //set payload
return msg; //return the msg

```

Notes:

- written on my phone (untested)
- No error checking

---

<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:** [13 October 2020 14:30 UTC](https://discourse.nodered.org/t/how-to-convert-object-into-array/31563/7 "2020-10-13T14:30:06Z")

</div>

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