user3915353
user3915353

Reputation: 9

parsing JSON with Telegraf

I newbe about Influx 2.0 and Telegraf and i'm tryng to parse my sensor data with this file.conf :

[[outputs.influxdb_v2]]

  urls = ["http://127.0.0.1:8086"`]
  token = ""
  organization = ""
  bucket = ""


[[inputs.http]]
 
  name_override = "mysensor"
  username = "xxxxxxx"
  password = "xxxxxxx"
  urls = [
    "http://mon.mysensors.it/sensor.php",

  ]

  insecure_skip_verify = true
  method = "GET"
  timeout = "5s"
  data_format = "json"

Here the .php page :

{
"getdata":"S",
"datetime":"16-05-2022 17:58:05",
"temp_sitoa":"20.7 C",
"temp_sitoa_state":"OK",
"temp_sitob":"24.1 C",
"temp_sitob_state":"WARNING",
"hum_sitob":"33%",
"hum_sitob_state":"OK",
"temp_sitoi":"19.9 C",
"temp_sitoi_state":"OK"
}

How can I get the the value of "temp_sitob" ? Thanks

Upvotes: 0

Views: 4074

Answers (1)

powersj
powersj

Reputation: 379

You are looking for the json_string_fields option of the json parser.

For a valid metric, you need to have one field and fields cannot be strings. If you look at your data, everything is a string. That is why no metrics are created. If you specify, however, using the json_string_fields option a string to use as a field, you can get data. I am using the file input to demonstrate this:

[[inputs.file]]
  files = ["data.json"]
  data_format = "json"
  json_string_fields = ["temp_sitob"]
file temp_sitob="24.1 C" 1653506475000000000

Ideally, your data will have some numerical data in it to more easily graph and chart in InfluxDB. You could also use the various processors in telegraf to convert the data types as well!

Hope that helps!

Upvotes: 1

Related Questions