diff --git a/plugins/parsers/json/README.md b/plugins/parsers/json/README.md index fa0d767ff..e1fce5550 100644 --- a/plugins/parsers/json/README.md +++ b/plugins/parsers/json/README.md @@ -108,7 +108,7 @@ Config: files = ["example"] name_key = "name" tag_keys = ["my_tag_1"] - string_fields = ["my_field"] + string_fields = ["b_my_field"] data_format = "json" ``` @@ -127,7 +127,7 @@ Input: Output: ``` -my_json,my_tag_1=foo a=5,b_c=6,my_field="description" +my_json,my_tag_1=foo a=5,b_c=6,b_my_field="description" ``` #### Arrays diff --git a/plugins/parsers/json/parser.go b/plugins/parsers/json/parser.go index 697296a12..1d8ce2d02 100644 --- a/plugins/parsers/json/parser.go +++ b/plugins/parsers/json/parser.go @@ -5,6 +5,8 @@ import ( "encoding/json" "fmt" "log" + "math" + "regexp" "strconv" "strings" "time" @@ -13,8 +15,6 @@ import ( "github.com/influxdata/telegraf/metric" "github.com/pkg/errors" "github.com/tidwall/gjson" - "math" - "regexp" ) var ( @@ -94,7 +94,6 @@ func parseUnixTimestamp(jsonValue interface{}, format string) (time.Time, error) } func (p *JSONParser) parseObject(metrics []telegraf.Metric, jsonOut map[string]interface{}) ([]telegraf.Metric, error) { - tags := make(map[string]string) for k, v := range p.DefaultTags { tags[k] = v @@ -108,7 +107,10 @@ func (p *JSONParser) parseObject(metrics []telegraf.Metric, jsonOut map[string]i //checks if json_name_key is set if p.JSONNameKey != "" { - p.MetricName = f.Fields[p.JSONNameKey].(string) + switch field := f.Fields[p.JSONNameKey].(type) { + case string: + p.MetricName = field + } } //if time key is specified, set it to nTime