How to forward to influx db only particular field from the incoming json

Hello,

I have the following type of metric being published on MQTT broker:

{
   "dev":[
      {
         "id":"office-32413",
         "l_seen":1659379835655,
         "mac":"deadbeefdead"
      },
      {
         "id":"office-56228",
         "l_seen":1659379835706,
         "mac":"deaddeaddead"
      }
   ],
   "n":2,
   "of":2
}

Pagination is here because the total count of the dev items may be super high, like thousands.

Before I added this pagination feature the message was just an array:

      {
         "id":"office-32413",
         "l_seen":1659379835655,
         "mac":"deadbeefdead"
      },
      {
         "id":"office-56228",
         "l_seen":1659379835706,
         "mac":"deaddeaddead"
      }
   ]

and the telegraf configuration for it that was perfectly working is:

[[inputs.mqtt_consumer]]
  servers = ["tcp://{{inventory_hostname}}:1883"]
  
  name_override = "engine-health-tags"
  qos = 0
  connection_timeout = 30
  topics = [
    "engine/+/h/+/t",
  ]
  persistent_session = true
  client_id = "engine-health-tags-telegraf"

  data_format = "json"
  json_string_fields = ["mac"]
  tag_keys = ["mac"]

What I am trying to achieve now is use Telegraf’s parser to get only the dev array from the incoming json and send it to Influxdb as a metric.

How should I change the above telegraf’s configuration to parse only the dev object from the paginated json so I keep the same data in the influx db as when there was no pagination?

Hi!

I would use the json_v2 parser to parse each object:

[[inputs.file]]
  files = ["./data.json"]

  data_format = "json_v2"
  [[inputs.file.json_v2]]
    [[inputs.file.json_v2.object]]
      path = "dev"
      tags = ["mac"]

For example, produced the following with your sample data:

file,mac=deadbeefdead id="office-32413",l_seen=1659379835655 1659381553000000000
file,mac=deaddeaddead id="office-56228",l_seen=1659379835706 1659381553000000000

The important part is the following, which I have updated from file to mqtt. You can then remove the data_format, json_string_fields, tag_keys keys and replace them with:

  data_format = "json_v2"
  [[inputs.mqtt.json_v2]]
    [[inputs.mqtt.json_v2.object]]
      path = "dev"
      tags = ["mac"]
1 Like

Thank you @jpowers ! It works perfectly !

1 Like