# How to store fields of arrays into InfluxDB using batch node? What formating function should I need?

**URL:** https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011
**Category:** General
**Created:** [7 June 2023 09:33 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011 "2023-06-07T09:33:03Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![mkdthanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mkdthanga/32/61384_2.png) [@mkdthanga](https://discourse.nodered.org/u/mkdthanga)
#### Post date: [7 June 2023 09:33 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/1 "2023-06-07T09:33:03Z")

</div>

I have the followiing payload with fields of arrays:

```auto
{"timestamp":1686127509414,
"topic":"NodeID;ns=3;s=\"V20Reg\".\"Pos\"",
"payload":[{"measurement":"V20RegPos",
"fields":[
{"rcpNbr":0,"stVisu":0,"batchNbr":0,"key":0,"stPlate":0,"note":0},
{"rcpNbr":0,"stVisu":0,"batchNbr":0,"key":0,"stPlate":0,"note":0},
{"rcpNbr":0,"stVisu":0,"batchNbr":0,"key":0,"stPlate":0,"note":0},
{"rcpNbr":0,"stVisu":8,"batchNbr":0,"key":0,"stPlate":0,"note":0},
{"rcpNbr":419,"stVisu":2,"batchNbr":3,"key":129,"stPlate":0,"note":0},
{"rcpNbr":419,"stVisu":2,"batchNbr":3,"key":129,"stPlate":0,"note":0},
{"rcpNbr":419,"stVisu":2,"batchNbr":3,"key":129,"stPlate":0,"note":0},
{"rcpNbr":419,"stVisu":6,"batchNbr":3,"key":128,"stPlate":0,"note":0},
{"rcpNbr":419,"stVisu":6,"batchNbr":3,"key":127,"stPlate":0,"note":0},
{"rcpNbr":419,"stVisu":6,"batchNbr":3,"key":127,"stPlate":0,"note":0},
.
.
.
{"rcpNbr":0,"stVisu":0,"batchNbr":0,"key":0,"stPlate":0,"note":0},
{"rcpNbr":0,"stVisu":0,"batchNbr":0,"key":0,"stPlate":0,"note":0},
{"rcpNbr":0,"stVisu":0,"batchNbr":0,"key":0,"stPlate":0,"note":0},
{"rcpNbr":0,"stVisu":0,"batchNbr":0,"key":0,"stPlate":0,"note":0},
{"rcpNbr":0,"stVisu":0,"batchNbr":0,"key":0,"stPlate":0,"note":0},
{"rcpNbr":0,"stVisu":0,"batchNbr":0,"key":0,"stPlate":0,"note":0},
{"rcpNbr":0,"stVisu":0,"batchNbr":0,"key":0,"stPlate":0,"note":0},
{"rcpNbr":0,"stVisu":0,"batchNbr":0,"key":0,"stPlate":0,"note":0}],
"timestamp":1686127509414000000}],
"_msgid":"517dfd98f7dc934c",
"measurement":"V20RegPos",
"statusCode":{"value":0},
"serverTimestamp":"2023-06-07T08:45:09.378Z",
"sourceTimestamp":"2023-06-07T08:45:09.378Z"}

```

When I try to parse the above payload through the InfluxDB Batch node, I get the following error from influxDB batch node:

```auto
Error: A 400 Bad Request error occurred: 
{"error":"unable to parse 'V20RegPos 
0=[object Object],1=[object Object],
10=[object Object],100=[object Object],
101=[object Object],102=[object Object],
103=[object Object],104=[object Object],
105=[object Object],106=[object Object],
107=[object Object],108=[object Object],
109=[object Object],11=[object Object],
110=[object Object],111=[object Object],
112=[object Object],113=[object Object],
114=[object Object],115=[object Object],
116=[object Object],117=[object Object],
118=[object Object],119=[object Object],
12=[object Object],120=[object Object],
121=[object Object],122=[object Object],
123=[object Object],124=[object Object],
125=[object Object],126=[object Object],
127=[object Object],128=[object Object],
129=[object Object],13=[object Object],
130=[object Object],131=[object Object],
132=[object Object],133=[object Object],
.
.
.
89=[object Object],9=[object Object],
90=[object Object],91=[object Object],
92=[object Object],93=[object Object],
94=[object Object],95=[object Object],
96=[object Object],97=[object Object],
98=[object Object],99=[object Object] 
1686129685511000000': invalid boolean"}

```

My Data formating Function:

```auto
var timestamp = msg.timestamp*1e6; //new Date().getTime() *1e6;
if (typeof msg.payload === "object"){ //if measurement/payload is a struct/object
msg.payload = [
    {
        measurement: msg.measurement,
        fields: msg.payload,
        timestamp: timestamp
    }
]
}
else{ // if the measurement/payload is singler var
    msg.payload = [
    {
        measurement: msg.measurement,
        fields: {value: msg.payload},
        timestamp: timestamp
    }
]
}

return msg;

```

My NodeRed Flow:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/2/f209627bfeaac7060282315ed367e5fb81836ca6.png)

Any Idea, what am I doing wrong here?

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [7 June 2023 09:57 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/2 "2023-06-07T09:57:52Z")

</div>

The `fields` property has to be an object of your field values, you are sending a _single point_ with `fields` as an array. That won't work.

You need to create a point _for each_ element of your array in `msg.payload`.

like that (untested)

```auto
msg.payload = msg.payload.map(el => {
    return {
        measurement: msg.measurement,
        fields: el,
        timestamp: timestamp
    }
});

```

It uses the [Array.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) function to convert your payload array into an array of Influx points.

---

<div class="post-metadata">

### Author: ![mkdthanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mkdthanga/32/61384_2.png) [@mkdthanga](https://discourse.nodered.org/u/mkdthanga)
#### Post date: [7 June 2023 11:06 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/3 "2023-06-07T11:06:32Z")

</div>

Then InfluxDB is storing only the last element/index per batch/timestamp, I want to store all the indeces values per timestamp. How can I do it?  
The structure of data stored in InfluxDB:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/47853c59e9499bbb43985f30f566346489f76500.png)

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [7 June 2023 11:35 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/4 "2023-06-07T11:35:12Z")

</div>

Ah, right. Points with the same timestamp will be overwritten. So you need to add a discriminator to make them unique.

I hope you are familiar with the [key concepts](https://docs.influxdata.com/influxdb/v1.8/concepts/key_concepts/) of InfluxDB.

So I suggest adding a _tag_ to each point that represents the position.

try this

```auto
msg.payload = msg.payload.map((el, idx) => {
    return {
        measurement: msg.measurement,
        tags: {
                position: idx
        },
        fields: el,
        timestamp: timestamp
    }
});

```

---

<div class="post-metadata">

### Author: ![mkdthanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mkdthanga/32/61384_2.png) [@mkdthanga](https://discourse.nodered.org/u/mkdthanga)
#### Post date: [7 June 2023 11:58 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/5 "2023-06-07T11:58:52Z")

</div>

@kuema Thanks a lot for your code and prompt answers. Now I got all the data with same timestamps inside InfluxDB, but now I don't know if its possible to display the arrays in grafana with its indices (of tags). Any Idea, how I can display this data with tags in grafana in a meaningful way?  
**Data Structure stored in InfluxDB:**  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/1/614dc919bc031c01100fcfcc3a5eea0b963dee4c.png)

---

<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: [7 June 2023 12:03 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/6 "2023-06-07T12:03:53Z")

</div>

What does the position in the array mean in real terms?

---

<div class="post-metadata">

### Author: ![mkdthanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mkdthanga/32/61384_2.png) [@mkdthanga](https://discourse.nodered.org/u/mkdthanga)
#### Post date: [7 June 2023 12:22 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/7 "2023-06-07T12:22:41Z")

</div>

its a station number (or position/coordinate) of a linear machine

---

<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: [7 June 2023 12:39 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/8 "2023-06-07T12:39:52Z")

</div>

Ok, so you have everything you need in the db. Exactly what output do you want to get?

---

<div class="post-metadata">

### Author: ![mkdthanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mkdthanga/32/61384_2.png) [@mkdthanga](https://discourse.nodered.org/u/mkdthanga)
#### Post date: [7 June 2023 12:57 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/9 "2023-06-07T12:57:16Z")

</div>

I want to display all the array elements with its timestamp in grafana, but there the options seemed to be limited with grafana query commands:

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

Any Idea on how to make an influx query so that I can display all the array elements in order with their timestamp in grafana?

---

<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: [7 June 2023 13:05 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/10 "2023-06-07T13:05:53Z")

</div>

Doesn't the previous screenshot you posted show that, apart from the fact that you have not sorted the index tag.

---

<div class="post-metadata">

### Author: ![mkdthanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mkdthanga/32/61384_2.png) [@mkdthanga](https://discourse.nodered.org/u/mkdthanga)
#### Post date: [7 June 2023 13:22 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/11 "2023-06-07T13:22:26Z")

</div>

Any Idea on how to sort the arrays with their index tag?

---

<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: [7 June 2023 13:26 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/12 "2023-06-07T13:26:15Z")

</div>

You are not sorting arrays, you are sorting records.

What do the influx docs say about sorting?

---

<div class="post-metadata">

### Author: ![mkdthanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mkdthanga/32/61384_2.png) [@mkdthanga](https://discourse.nodered.org/u/mkdthanga)
#### Post date: [13 June 2023 11:09 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/14 "2023-06-13T11:09:23Z")

</div>

I have used the Influx Query with 'Where' Clause as follows:

```auto
SELECT distinct("${variabel}") FROM "V20RegPos" WHERE ("position" = '0') AND $timeFilter GROUP BY time($__interval) fill(none)

SELECT distinct("${variabel}") FROM "V20RegPos" WHERE ("position" = '1') AND $timeFilter GROUP BY time($__interval) fill(none)
.
.
.

SELECT distinct("${variabel}") FROM "V20RegPos" WHERE ("position" = '319') AND $timeFilter GROUP BY time($__interval) fill(none)

```

---

<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: [18 June 2023 16:12 UTC](https://discourse.nodered.org/t/how-to-store-fields-of-arrays-into-influxdb-using-batch-node-what-formating-function-should-i-need/79011/15 "2023-06-18T16:12:05Z")

</div>

I am away at the moment, but I think it is order-by or similar
