influxdata / telegraf

Agent for collecting, processing, aggregating, and writing metrics, logs, and other arbitrary data.
https://influxdata.com/telegraf
MIT License
14.59k stars 5.56k forks source link

JSON Parsing #4943

Closed orcunbaslak closed 5 years ago

orcunbaslak commented 5 years ago

Hello,

I'm trying to parse a JSON at Telegraf and failing at it.

JSON is like this.

{
  "timestamp": 1541021024224,
  "values": [
    {
      "id": "Raspberry.Pi.Ucuncu",
      "t": 1541021024193,
      "v": 7
    },
    {
      "id": "Raspberry.Pi.Dorduncu",
      "t": 1541021024193,
      "v": 0
    },
    {
      "id": "Raspberry.Pi.Ikinci",
      "t": 1541021024193,
      "v": 4
    },
    {
      "id": "Raspberry.Pi.Birinci",
      "t": 1541021024193,
      "v": 3
    }
  ]
}

and my Telegraf config is like this

#KEPSERVER
[[inputs.mqtt_consumer]]
  servers = ["tcp://localhost:1883"]
  qos = 0
  connection_timeout = "30s"
  topics = [
    "kesy",
  ]
  persistent_session = true
  client_id = "OrcunMQTT"
  data_format = "json"
  name_suffix = "_kesy_vm"

In the json; id will be the tag, t will be the timestamp and v is the value of the tag. timestamp at the top can be ignored.

Any help on this structure is very much appreciated.

Br,

danielnelson commented 5 years ago

@orcunbaslak Try these settings out:

[[inputs.mqtt_consumer]]
  # ... snip...
  json_query = "values"
  tag_keys = ["id"]
  json_time_key = "t"
  json_time_format = "unix_ms"
  data_format = "json"
  fielddrop = ["t"]
danielnelson commented 5 years ago

The fielddrop is needed because the time_key is not removed from the output even after being used. I think this is unwanted behavior so I created #4950.