简体   繁体   English

用 Telegraf 解析 JSON

[英]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:我是 Influx 2.0 和 Telegraf 的新手,我正在尝试使用此 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:这里是.php页面:

{
"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"?我怎样才能得到“temp_sitob”的价值? Thanks谢谢

You are looking for the json_string_fields option of the json parser .您正在寻找json 解析器json_string_fields选项。

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.但是,如果您使用json_string_fields选项指定一个字符串用作字段,则可以获得数据。 I am using the file input to demonstrate this:我正在使用file输入来演示这一点:

[[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.理想情况下,您的数据中将包含一些数字数据,以便更轻松地在 InfluxDB 中绘制图表。 You could also use the various processors in telegraf to convert the data types as well!您也可以使用 telegraf 中的各种处理器来转换数据类型!

Hope that helps!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM