Process an array of json data

I have setup the “mqtt_consume” input plugin for my telegraf agent. The configuration is shown below:

[[inputs.mqtt_consumer]]
      alias = "input_mqtt_consumer_tsesre_node"
      servers = [
        "mqtt://constellation-gateway-emqx.test.svc.cluster.local:1883"
      ]
      topics = [
        "foo/edge/gms/tsesre/postmetrics/true"
      ]
      persistent_session = false
      username = "test"
      password = "1234"
      json_strict = true
      tag_keys = [
        "tags_*"
      ]
      data_format = "json"
      json_name_key = "measurement"
      json_query = "metrics"
      json_string_fields = [
        "fields_error",
        "fields_success"
      ]
      json_time_format = "unix"
      json_time_key = "epochtime"
      json_timezone = "UTC"
      [inputs.mqtt_consumer.tags]
        tags_for_org = "tsesre"
        tags_company = "REPLACE_WITH_COMPANY_NAME"
        tags_node = "REPLACE_WITH_NODE_NAME"
        tags_site = "REPLACE_WITH_SITE_NAME"

And here’s the example of my json data which it is getting from the mqtt broker:

[
  {
    "metrics": [
      {
        "epochtime": 1721039581,
        "fields": {
          "error": "",
          "success": true
        },
        "measurement": "measurement_test",
        "tags": {
          "bucket_name": "test-bucket",
          "destination": "events",
          "host": "dddev08.abc.com"
        }
      },
      {
        "epochtime": 1721039681,
        "fields": {
          "error": "test error",
          "success": false
        },
        "measurement": "measurement_test",
        "tags": {
          "bucket_name": "test-bucket",
          "destination": "testResults",
          "host": "ddddev08.abc.com"
        }
      }
    ]
  }
]

With the above configuration it is unable to process the json data.
What modifications should i make in the configurations?

Thanks

The JSON parser is only good for flat data. I would suggest taking a look at the xpath parser, specifically the last example titled “JSON Line Protocol”: telegraf/docs/PARSING_DATA.md at master · influxdata/telegraf · GitHub and adapting it to your data.

1 Like