How to get an json array into InfluxDB Table with Telegraf

Hey Guys,

I am trying to feed a MQTT-Message in json_v2 format which contains an array of integers into InfluxDB. Currently it just takes the last number of the array and all others are lost.

if the array in the message looks like this:

    "data" : {
        "energy" : [11,12,13]

The config looks like this:

    rename = "energy_list"
    path = ""
    data_type = "array"

Influx will only give me the “13” in the table, but Id like to have it “11, 12, 13”

Hello @Nils_Franke,
So this documentation might be helpful.

I would try .object and maybe the prepend too.
I think that should get you all of your metrics. Though I wonder if/bet that they’re being overwritten. ’
Yup just confirmed with the --test flag and printed lines to stdout

> file,host=MacBook-Pro-4.local energy=11 1706728355000000000
> file,host=MacBook-Pro-4.local energy=12 1706728355000000000
> file,host=MacBook-Pro-4.local energy=13 1706728355000000000

You would need to add a timestamp to each energy value. or make energy a tag (which is not ideal).

You can add a timestamp with the execd processor plugin.
Your python script for example could look like:

import sys
import json
from datetime import datetime, timedelta

def add_timestamps(data):
    base_time =
    if 'energy' in data:
        for i, value in enumerate(data['energy']):
            data['energy'][i] = {
                "value": value,
                "timestamp": (base_time + timedelta(seconds=i)).isoformat() + "Z"
    return data

# Read JSON from stdin
input_data = json.load(sys.stdin)

# Process the data
output_data = add_timestamps(input_data)

# Write JSON to stdout
json.dump(output_data, sys.stdout)

Your telegraf config would be:

    files = ["./tel_test.json"]
    data_format = "json_v2"
            path = "data"
#             disable_prepend_keys = true
  command = ["python3", "./"]
  data_format = "json"

and your would change the timestamps slightly.

Or you could make 3 field keys this is the better solution i’m guessing. Still the approach could be the same. Youd just change the logic in your python script.

1 Like

@Nils_Franke so there is a way to do this with xpath apparently.

Wooo! Thank you for asking

Hey Anaisdg,

thank you for your quick answers. I actually came along these posts before reaching out to you and they are not very helpful to be honest. Not least because it says “Closed, solution found” without pointing out the solution. Every example, also on other webpages, shows only line protocol outputs, but we want to have it being shown correctly in the Influx->Data Explorer-> “Simple Table”. Can you give me a doc or a step-by-step guide on this one?

Thzanks for your support,