JSON 输入数据格式
以下信息适用于旧版 JSON 输入数据格式。对于大多数情况,请改用 JSON v2 输入数据格式。
json
数据格式将 JSON 对象或对象数组解析为指标字段。
注意: 所有 JSON 数字都转换为浮点字段。除非在 tag_key
或 json_string_fields
选项中指定,否则 JSON 字符串和布尔值将被忽略。
配置
[[inputs.file]]
files = ["example"]
## Data format to consume.
## Each data format has its own unique set of configuration options, read
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
data_format = "json"
## When strict is true and a JSON array is being parsed, all objects within the
## array must be valid
json_strict = true
## Query is a GJSON path that specifies a specific chunk of JSON to be
## parsed, if not specified the whole document will be parsed.
##
## GJSON query paths are described here:
## https://github.com/tidwall/gjson/tree/v1.3.0#path-syntax
json_query = ""
## Tag keys is an array of keys that should be added as tags. Matching keys
## are no longer saved as fields. Supports wildcard glob matching.
tag_keys = [
"my_tag_1",
"my_tag_2",
"tags_*",
"tag*"
]
## Array of glob pattern strings or booleans keys that should be added as string fields.
json_string_fields = []
## Name key is the key to use as the measurement name.
json_name_key = ""
## Time key is the key containing the time that should be used to create the
## metric.
json_time_key = ""
## Time format is the time layout that should be used to interpret the json_time_key.
## The time must be `unix`, `unix_ms`, `unix_us`, `unix_ns`, or a time in the
## "reference time". To define a different format, arrange the values from
## the "reference time" in the example to match the format you will be
## using. For more information on the "reference time", visit
## https://golang.ac.cn/pkg/time/#Time.Format
## ex: json_time_format = "Mon Jan 2 15:04:05 -0700 MST 2006"
## json_time_format = "2006-01-02T15:04:05Z07:00"
## json_time_format = "01/02/2006 15:04:05"
## json_time_format = "unix"
## json_time_format = "unix_ms"
json_time_format = ""
## Timezone allows you to provide an override for timestamps that
## don't already include an offset
## e.g. 04/06/2016 12:41:45
##
## Default: "" which renders UTC
## Options are as follows:
## 1. Local -- interpret based on machine localtime
## 2. "America/New_York" -- Unix TZ values like those found in https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
## 3. UTC -- or blank/unspecified, will return timestamp in UTC
json_timezone = ""
json_query
json_query
是一个 GJSON 路径,可用于在解析之前转换 JSON 文档。查询在应用任何其他选项之前执行,并且将解析生成的新文档而不是原始文档,因此,查询的结果应为 JSON 对象或对象数组。
有关详细信息和示例,请参阅 GJSON 路径语法,并考虑使用 GJSON playground 来开发和调试您的查询。
json_time_key, json_time_format, json_timezone
默认情况下,当前时间将用于所有创建的指标,要使用 JSON 文档设置时间,您可以同时使用 json_time_key
和 json_time_format
选项将时间设置为解析文档中的值。
json_time_key
选项指定包含时间值的键,json_time_format
必须设置为 unix
、unix_ms
、unix_us
、unix_ns
或 Go “参考时间”,其定义为特定时间:Mon Jan 2 15:04:05 MST 2006
。
有关如何设置时间格式的详细信息和其他示例,请参阅 Go time 包。
当解析不包含时区说明符的时间时,时间假定为 UTC。要默认为另一个时区或本地时间,请指定 json_timezone
选项。此选项应设置为 Unix TZ 值,例如 America/New_York
,设置为 Local
以使用系统时区,或设置为 UTC
。
示例
基本解析
配置
[[inputs.file]]
files = ["example"]
name_override = "myjsonmetric"
data_format = "json"
输入
{
"a": 5,
"b": {
"c": 6
},
"ignored": "I'm a string"
}
输出
myjsonmetric a=5,b_c=6
名称、标签和字符串字段
配置
[[inputs.file]]
files = ["example"]
json_name_key = "name"
tag_keys = ["my_tag_1"]
json_string_fields = ["b_my_field"]
data_format = "json"
输入
{
"a": 5,
"b": {
"c": 6,
"my_field": "description"
},
"my_tag_1": "foo",
"name": "my_json"
}
输出
my_json,my_tag_1=foo a=5,b_c=6,b_my_field="description"
数组
如果 JSON 数据是一个数组,则数组中的每个对象都将使用配置的设置进行解析。
配置
[[inputs.file]]
files = ["example"]
data_format = "json"
json_time_key = "b_time"
json_time_format = "02 Jan 06 15:04 MST"
输入
[
{
"a": 5,
"b": {
"c": 6,
"time":"04 Jan 06 15:04 MST"
}
},
{
"a": 7,
"b": {
"c": 8,
"time":"11 Jan 07 15:04 MST"
}
}
]
输出
file a=5,b_c=6 1136387040000000000
file a=7,b_c=8 1168527840000000000
查询
json_query
选项可用于解析文档的子集。
配置
[[inputs.file]]
files = ["example"]
data_format = "json"
tag_keys = ["first"]
json_string_fields = ["last"]
json_query = "obj.friends"
输入
{
"obj": {
"name": {"first": "Tom", "last": "Anderson"},
"age":37,
"children": ["Sara","Alex","Jack"],
"fav.movie": "Deer Hunter",
"friends": [
{"first": "Dale", "last": "Murphy", "age": 44},
{"first": "Roger", "last": "Craig", "age": 68},
{"first": "Jane", "last": "Murphy", "age": 47}
]
}
}
输出
file,first=Dale last="Murphy",age=44
file,first=Roger last="Craig",age=68
file,first=Jane last="Murphy",age=47
此页是否对您有帮助?
感谢您的反馈!