Elements of json array as fields

Maxime
Thu, 16 Jan 2025 22:17:57 +0000
Telegraf

Hi,
Consider the following json (from an MQTT message):

{
  "device_id": "sp-e4a465",
  "mac": "AA:BB:CC:11:22:33",
  "timestamp": 1736666999,
  "metrics": [
    {"name": "co2", "value": 450},
    {"name": "temperature", "value": 22.5}
  ]
}

I’d like to store it like this in InfluxDB:

time | device_id (tag) | co2 (field) | temperature (field)
1736666999 | sp-e4a465 | 450 | 22.5

Because I think it’s the most logical way to store this data. Each message can have an a arbitrary number of metrics in the array, with arbitrary metric names, so everything must be dynamic.

This question here json - Telegraf json_string_fields for string arrays - Stack Overflow is similar, but in addition, I also need to name the fields themselves according to the “name” value in the json.

I’m sure it’s possible using json_v2 or xpath but I’m new to Telegraf and can’t figure it out. Thank you for your help.

1 post - 1 participant