# Send JSON data to influxdb from Node-red

**URL:** https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230
**Category:** General
**Created:** [15 February 2023 11:41 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230 "2023-02-15T11:41:25Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![dhanushmh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dhanushmh/32/75899_2.png) [@dhanushmh](https://discourse.nodered.org/u/dhanushmh)
#### Post date: [15 February 2023 11:41 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/1 "2023-02-15T11:41:25Z")

</div>

Can someone help me to send JSON data to Influxdb from node-red. You can find my output data in the attached image and the influx node I have. I am using influx 2.0 version. My data will change every 10secs so, I can't hard code anything here. Thank you 🙂

 ![Influx](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0c6cdaafb15ecec050255effbe4539e27fee83aa.png)

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [15 February 2023 13:25 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/2 "2023-02-15T13:25:02Z")

</div>

Start by removing the json node.

Influx requires a measument, fields, values and timestamp - and unless you are not using them for aggregations, no strings.

I am using the influx batch node myself, which accepts arrays of timeseries measurements.

Example how I insert data (note the `tag` to distinguish between my indoor/outdoor measurements):

```auto
[
    {
        "measurement": "netatmo",
        "fields":
        {
            "time_utc": 1676466703,
            "Temperature": 18.9,
            "CO2": 609,
            "Humidity": 57,
            "Noise": 39,
            "Pressure": 1022.2,
            "AbsolutePressure": 1021.8,
            "min_temp": 18.8,
            "max_temp": 20.7,
            "date_max_temp": 1676418222,
            "date_min_temp": 1676449778,
            "temp_trend": "stable",
            "pressure_trend": "down"
        },
        "tags":
        {
            "location": "indoor"
        },
        "timestamp": "2023-02-15T13:17:51.559Z"
    },
    {
        "measurement": "netatmo",
        "fields":
        {
            "time_utc": 1676466674,
            "Temperature": 11.7,
            "Humidity": 71,
            "min_temp": 3.3,
            "max_temp": 11.7,
            "date_max_temp": 1676466674,
            "date_min_temp": 1676440681,
            "temp_trend": "up",
            "battery": 87,
            "battery_vp": 5774
        },
        "tags":
        {
            "location": "outdoor"
        },
        "timestamp": "2023-02-15T13:17:51.559Z"
    }
]

```

So you will need to redefine your message/arrays.

---

<div class="post-metadata">

### Author: ![dhanushmh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dhanushmh/32/75899_2.png) [@dhanushmh](https://discourse.nodered.org/u/dhanushmh)
#### Post date: [15 February 2023 13:34 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/3 "2023-02-15T13:34:29Z")

</div>

Thank you, but how can I generalise the code so that values are taken from ESP32? It would be a real-time data coming from ESP32.

---

<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: [15 February 2023 13:35 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/4 "2023-02-15T13:35:28Z")

</div>

Show us what the data from the esp looks like?

---

<div class="post-metadata">

### Author: ![dhanushmh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dhanushmh/32/75899_2.png) [@dhanushmh](https://discourse.nodered.org/u/dhanushmh)
#### Post date: [15 February 2023 13:39 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/5 "2023-02-15T13:39:57Z")

</div>

![ESp32_Output](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/c/3c0e01fd9b308ed171c3b96a16d140154e4b0f77.png)  
This is what my esp32 output looks like, I have two device\_IDs, and every 5secs I get data from these two devices which come as a string.

As you can see in the first image, I just split the data and put it in JSON so that I can send it to AWS, but I need to do it with influxdb.

---

<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: [15 February 2023 14:05 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/6 "2023-02-15T14:05:36Z")

</div>

Do you have control of the esp code? If so then I suggest you change it to return a JSON string that you can feed into a JSON node to give you a javascript object.

In the debug it shows a whole array of data, are you combining multiple readings together? If so then don't bother, just send them one at a time to influxdb, with device id as a tag and temperature, humidity and wind speed as fields.

---

<div class="post-metadata">

### Author: ![dhanushmh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dhanushmh/32/75899_2.png) [@dhanushmh](https://discourse.nodered.org/u/dhanushmh)
#### Post date: [15 February 2023 14:15 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/7 "2023-02-15T14:15:54Z")

</div>

```auto
// Parse the incoming message data
var data = msg.payload;
var parts = data.split(';');
var geodata = parts[0];
var device_id = parts[1];
var temperature = parts[2];
var humidity = parts[3];
var wind_speed = parts[4];

// Build the InfluxDB data point
var timestamp = new Date().toISOString();
var measurement = "windanalysis";
var fields = {
    "temperature": parseFloat(temperature.split(':')[1]),
    "humidity": parseFloat(humidity.split(':')[1]),
    "wind_speed": parseFloat(wind_speed.split(':')[1])
};
var tags = {
    "geodata": geodata.split(':')[1],
    "device_id": device_id.split(':')[1]
};

// Send the data point to InfluxDB
msg.payload = [{ measurement: measurement, fields: fields, tags: tags, timestamp: timestamp }];
return msg;

```

I did change the code like this, but even then I am not able to see anything in Influxdb, none of the fields getting generated in Influxdb

 ![Influxdb](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/f/5fbdca0592ea370eca83341bd39bb9a3c8b24705.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: [15 February 2023 14:31 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/8 "2023-02-15T14:31:32Z")

</div>

Assuming you are sending to the Influx Out node then look in the help text and you will see that is not a valid structure for the data.  
You need the measurement in msg.measurement, and in msg.payload you need

```auto
[
  {
    field1: value,
    field2: value
  },
  {
    tag1: value,
    tag2: value
  }
]

```

You don't need to provide a timestamp, influx will add the current time automatically

---

<div class="post-metadata">

### Author: ![dhanushmh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dhanushmh/32/75899_2.png) [@dhanushmh](https://discourse.nodered.org/u/dhanushmh)
#### Post date: [15 February 2023 14:46 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/9 "2023-02-15T14:46:43Z")

</div>

I have used the same syntax, I am just calling the measurement, field, and tags from the above piece of code. I tried doing it the same way as you mentioned and still, it doesn't give any output in influxdb.

---

<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: [15 February 2023 14:54 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/10 "2023-02-15T14:54:36Z")

</div>

The code you showed does not give anything like the structure I posted. Feed your output into a debug node and show us what it looks like.

**[Edit]** I mean show us what you are feeding to the influx node.

---

<div class="post-metadata">

### Author: ![dhanushmh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dhanushmh/32/75899_2.png) [@dhanushmh](https://discourse.nodered.org/u/dhanushmh)
#### Post date: [15 February 2023 14:58 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/11 "2023-02-15T14:58:00Z")

</div>

```auto
// Parse the incoming message data
var data = msg.payload;
var parts = data.split(';');
var geodata = parts[0];
var device_id = parts[1];
var temperature = parts[2];
var humidity = parts[3];
var wind_speed = parts[4];

// Send the data point to InfluxDB
[{ 
    measurement: "windanalysis",
    fields:
    {
        "temperature": parseFloat(temperature.split(':')[1]),
        "humidity": parseFloat(humidity.split(':')[1]),
        "wind_speed": parseFloat(wind_speed.split(':')[1])
    }, 
    tags:
    {
        "geodata": geodata.split(':')[1],
        "device_id": device_id.split(':')[1]
    }
}];
return msg;

```

This is the code right now, I believe this is according to the syntax.

Debug looks like this after this function node

```auto
geodata:15.34-34.67;device_ID:3;Temperature:30 degC;Humidity:33 RH;WindSpeed:333 m/s

```

---

<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: [15 February 2023 14:58 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/12 "2023-02-15T14:58:46Z")

</div>

> [@dhanushmh](#):
>
> Debug looks like this after this function node

Does that look like the structure I posted?

---

<div class="post-metadata">

### Author: ![dhanushmh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dhanushmh/32/75899_2.png) [@dhanushmh](https://discourse.nodered.org/u/dhanushmh)
#### Post date: [15 February 2023 15:08 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/13 "2023-02-15T15:08:51Z")

</div>

> [@Colin](#):
>
> You need the measurement in msg.measurement, and in msg.payload you need

Could you please how can I change this in my code? I am a bit new to node-red and I am not understanding how can I write this. Sorry for the simple questions.

---

<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: [15 February 2023 16:33 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/14 "2023-02-15T16:33:19Z")

</div>

Something like

```auto
msg.measurement = "windanalysis"
msg.payload = [
  {
    temperature: parseFloat(temperature.split(':')[1]),
    humidity: parseFloat(humidity.split(':')[1]),
    etc
  },
  {
    geodata: ...
    ...
  }
]

```

Check it looks right by feeding it into a debug node before you try sending it to influx. Set the debug node to Show Full Message so that you can see msg.measurement.  
If it still isn't right then show us the new function and show us what you see in the debug node.

---

<div class="post-metadata">

### Author: ![dhanushmh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dhanushmh/32/75899_2.png) [@dhanushmh](https://discourse.nodered.org/u/dhanushmh)
#### Post date: [15 February 2023 18:41 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/15 "2023-02-15T18:41:38Z")

</div>

Yeah, I misunderstood what you told me in the beginning. I had confusion batch mode and normal ones. I followed it again it worked perfectly fine. Thank you for the 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: [1 March 2023 18:42 UTC](https://discourse.nodered.org/t/send-json-data-to-influxdb-from-node-red/75230/16 "2023-03-01T18:42:01Z")

</div>

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