# Read fields from arrays in object

**URL:** <https://discourse.nodered.org/t/read-fields-from-arrays-in-object/8339>\
**Category:** General\
**Created:** [24 February 2019 19:36 UTC](https://discourse.nodered.org/t/read-fields-from-arrays-in-object/8339 "2019-02-24T19:36:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![innotractor](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@innotractor](https://discourse.nodered.org/u/innotractor)\
**Post date:** [24 February 2019 19:36 UTC](https://discourse.nodered.org/t/read-fields-from-arrays-in-object/8339/1 "2019-02-24T19:36:14Z")

</div>

Hi,

I am new into node-red and JavaScript. I am trying to read an object consisting of multiple arrays and insert some fields per array into a sql database (Sqllite). I have tried the split node and ended with trying to create some JavaScript consisting of a for loop to read the arrays and extract the fields.....

If anybody could give me the hint, I would be very grateful, thx in advance

See attachement for the structure of my object

 ![Object_structure](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/1e5c027047c781d106ac0b549b60c457cad51a55.jpeg)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [24 February 2019 19:39 UTC](https://discourse.nodered.org/t/read-fields-from-arrays-in-object/8339/2 "2019-02-24T19:39:07Z")

</div>

and the javascript you tried?

---

<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:** [24 February 2019 20:21 UTC](https://discourse.nodered.org/t/read-fields-from-arrays-in-object/8339/3 "2019-02-24T20:21:06Z")

</div>

What are you trying to get out of the data?

---

<div class="post-metadata">

**Author:** ![innotractor](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@innotractor](https://discourse.nodered.org/u/innotractor)\
**Post date:** [24 February 2019 21:27 UTC](https://discourse.nodered.org/t/read-fields-from-arrays-in-object/8339/4 "2019-02-24T21:27:11Z")

</div>

var i;  
var sql="";  
var outputs=[];  
var id;  
var name;

for(i=0; i\<payload.NbrOfRows; i++) {  
id=msg.payload.Rows[i].Id;  
name=msg.payload.Rows[i].Name;  
sql="INSER INTO devices(id,name) " +  
"VALUES ("+id+","+name+")";

```
outputs.push=({topic:sql});

```

}

This is what I have tried...composed from different inputs 😀

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [24 February 2019 21:46 UTC](https://discourse.nodered.org/t/read-fields-from-arrays-in-object/8339/5 "2019-02-24T21:46:08Z")

</div>

For a start  
`payload.NbrOfRows` does what? to what?

If you mean the length of the Array look at [https://www.w3schools.com/jsref/jsref\_length\_array.asp](https://www.w3schools.com/jsref/jsref_length_array.asp)

Rather than try to do it all at once break it down into easy chunks. do each chunk one at a time.

So start by getting the javascript correct for the length of the array. You might also want to look up the SQL query to add an entry into a database table

---

<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:** [24 February 2019 21:55 UTC](https://discourse.nodered.org/t/read-fields-from-arrays-in-object/8339/6 "2019-02-24T21:55:28Z")

</div>

> [@innotractor](#):
>
> outputs.push=({topic:sql});

The `=` should not be there.

> [@innotractor](#):
>
> sql="INSER INTO

That should be `INSERT`

Then if you get an error from the function that you don't understand show us what it is. Otherwise put a debug on the output and see what it says.  
What are you doing at the end of the function? If you want to send multiple messages to one output then it needs to be an array containing an array so you would need something like  
`return [outputs]`

---

<div class="post-metadata">

**Author:** ![innotractor](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@innotractor](https://discourse.nodered.org/u/innotractor)\
**Post date:** [25 February 2019 07:52 UTC](https://discourse.nodered.org/t/read-fields-from-arrays-in-object/8339/7 "2019-02-25T07:52:34Z")

</div>

Hi,

It's working now, except for my typos and missing "msg.", I needed to place some "" around the id and name. This is the final working code:

```auto
var i;
var sql="";
var outputs=[];
var id;
var name;

for (i=0; i< msg.payload.NbrOfRows; i++) {
    id=msg.payload.Rows[i].Id;
    name=msg.payload.Rows[i].Name;
    sql="INSERT OR REPLACE INTO devices(id,name) " + 
    "VALUES (\""+id+"\",\""+name+"\");";
    outputs.push({topic:sql});
}

return [outputs];

```
