# Array to Json Object

**URL:** <https://discourse.nodered.org/t/array-to-json-object/46261>\
**Category:** General\
**Created:** [26 May 2021 09:20 UTC](https://discourse.nodered.org/t/array-to-json-object/46261 "2021-05-26T09:20:07Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [26 May 2021 09:36 UTC](https://discourse.nodered.org/t/array-to-json-object/46261/2 "2021-05-26T09:36:40Z")

</div>

Hi @jackie7

> [@jackie7](#):
>
> I want to convert an array to a Json object

Sorry to nitpick but Json object is not a thing.  
JSON is a string representation of a JavaScript object.

Ok, enough of that 🙂

Use a function node, loop through the elements & add properties to an object...

```auto
  var arr = msg.payload; // get the array
  var rv = {};// create a new empty object
  for (var i = 0; i < arr.length; ++i) {
    let key = "Val"+(i+1); //built a key 
    rv[key] = arr[i]; // set value in New object
  }
  msg.payload = nv;
  return msg;

```

There are other, more modern/compact ways to achieve this but I kept it simple for clarity.

Disclaimer: the above code is untested/off the top of my head. 😇

---

_[View the full topic](https://discourse.nodered.org/t/array-to-json-object/46261)._
