# Creating json object in js file

**URL:** <https://discourse.nodered.org/t/creating-json-object-in-js-file/31623>\
**Category:** Developing Nodes\
**Created:** [16 August 2020 20:09 UTC](https://discourse.nodered.org/t/creating-json-object-in-js-file/31623 "2020-08-16T20:09:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nodiaque](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@nodiaque](https://discourse.nodered.org/u/nodiaque)\
**Post date:** [16 August 2020 20:09 UTC](https://discourse.nodered.org/t/creating-json-object-in-js-file/31623/1 "2020-08-16T20:09:06Z")

</div>

Hello everyone,

I'm currently trying to parse some data I receive from a request to send only what I want to the html file. I manage to get the data like I want, but when I try to create my object, I can't parse it properly. Here's what I currently have as JS script:

```auto
var itemList = [];
items.thermostatList.forEach(function (value,index){
	if (value.name.trim() != ""){
		itemList += {
			"value": "Thermostat: " + value.name
		};
			
	}
	value.remoteSensors.forEach( function (val, ind){
	});
					
});
console.log("itemList: " + itemList);
var keyInfo = Object.keys(itemList);
console.log("itemList info: " + keyInfo);

```

Here's the console output:

```auto
itemList: [object Object]
itemList info: 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14

```

Now, problem is the keys are number. How do I access that?

I tried itemList.forEach(....) and that failed saying forEach is not a function of itemList.  
I tried itemList[0] and itemList.0, both failed to compiled. The goal here is to finish with:

```auto
var keys = { 
	result:"ok", 
	items : itemList
};
res.json(keys);
return;

```

So afterward, on my html file, I can parse the json and get all items.

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:** [16 August 2020 20:26 UTC](https://discourse.nodered.org/t/creating-json-object-in-js-file/31623/2 "2020-08-16T20:26:58Z")

</div>

> [@nodiaque](#):
>
> ```auto
> var itemList = [];
> items.thermostatList.forEach(function (value,index){
> if (value.name.trim() != ""){
> itemList += {
> "value": "Thermostat: " + value.name
> };
> 			
> }
> value.remoteSensors.forEach( function (val, ind){
> });
> 					
> });
> console.log("itemList: " + itemList);
> var keyInfo = Object.keys(itemList);
> console.log("itemList info: " + keyInfo);
> 
> ```

An array is an object and when you do this...

```auto
        itemList += {
			"value": "Thermostat: " + value.name
		};

```

I dont think it is having the affect you want (or maybe incidentally it is!)

Any how, to add to an array, you use push

e.g...

```auto
itemList.push({
    "value": "Thermostat: " + value.name
});

```

Then you can access items as an array `itemList[0]` and `itemList[0].value` etc.

---

<div class="post-metadata">

**Author:** ![nodiaque](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@nodiaque](https://discourse.nodered.org/u/nodiaque)\
**Post date:** [20 August 2020 20:13 UTC](https://discourse.nodered.org/t/creating-json-object-in-js-file/31623/3 "2020-08-20T20:13:24Z")

</div>

Hello, thanks a lot for the information, it worked perfectly!

---

<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:** [3 September 2020 20:13 UTC](https://discourse.nodered.org/t/creating-json-object-in-js-file/31623/4 "2020-09-03T20:13:31Z")

</div>

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