# 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:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackie7](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jackie7](https://discourse.nodered.org/u/jackie7)\
**Post date:** [26 May 2021 09:20 UTC](https://discourse.nodered.org/t/array-to-json-object/46261/1 "2021-05-26T09:20:07Z")

</div>

Hey there,  
I want to convert an array to a Json object. I've tried many ways but i didn't get the format I want!  
I want to get the following result:  
payload: " {"Val1" : 10, "Val2" : 11, "Val3: 12} "

Plus, I have a subflow that returns a value, how can I put this value in a Json object, say "Value1" : 20 ?

thank you,

---

<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. 😇

---

<div class="post-metadata">

**Author:** ![jackie7](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jackie7](https://discourse.nodered.org/u/jackie7)\
**Post date:** [26 May 2021 10:08 UTC](https://discourse.nodered.org/t/array-to-json-object/46261/3 "2021-05-26T10:08:48Z")

</div>

Thank you for the clarification!  
I'm able to parse the array and get the values but I want to assign names to them, each value has a different name. how can I change their names from Val1 Val2...?

---

<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 10:33 UTC](https://discourse.nodered.org/t/array-to-json-object/46261/4 "2021-05-26T10:33:50Z")

</div>

> [@jackie7](#):
>
> each value has a different name. how can I change their names from Val1 Val2.

unless you tell me what they are I cannot really help. an array is just a group of values.

If you know which element is which item & they are always in the same place & there are always a fixed number of elements in the array, you can simply set them manually in the object.

However, there may be a better solution - we should look at that first.

Where does this array come from? can you show me how this array is generated?

---

<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 May 2021 10:41 UTC](https://discourse.nodered.org/t/array-to-json-object/46261/5 "2021-05-26T10:41:33Z")

</div>

You would need to list the names

```auto
var names = ["name1","another_name","any_anme_you_wish"];// list of names
var hold_obj = {};// `temp object
names.forEach ((v,i) => hold_obj[v] = msg.payload[i]) //loop through names and add to object
msg.payload = hold_obj; // move back to payload
return msg;

```

assuming the array is in msg.payload.

---

<div class="post-metadata">

**Author:** ![jackie7](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jackie7](https://discourse.nodered.org/u/jackie7)\
**Post date:** [26 May 2021 11:16 UTC](https://discourse.nodered.org/t/array-to-json-object/46261/6 "2021-05-26T11:16:57Z")

</div>

That was helpful, thank you very much!!

---

<div class="post-metadata">

**Author:** ![jackie7](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jackie7](https://discourse.nodered.org/u/jackie7)\
**Post date:** [26 May 2021 11:17 UTC](https://discourse.nodered.org/t/array-to-json-object/46261/7 "2021-05-26T11:17:55Z")

</div>

This was solved, Thank you for your help!

---

<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:** [9 June 2021 11:18 UTC](https://discourse.nodered.org/t/array-to-json-object/46261/8 "2021-06-09T11:18:40Z")

</div>

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