Skip to content

Commit 34db8b8

Browse files
glintondanielnelson
authored andcommitted
Fix panic if JSONNameKey is not found (#4735)
(cherry picked from commit 4c9c31c)
1 parent 4fdf946 commit 34db8b8

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

plugins/parsers/json/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Config:
108108
files = ["example"]
109109
name_key = "name"
110110
tag_keys = ["my_tag_1"]
111-
string_fields = ["my_field"]
111+
string_fields = ["b_my_field"]
112112
data_format = "json"
113113
```
114114

@@ -127,7 +127,7 @@ Input:
127127

128128
Output:
129129
```
130-
my_json,my_tag_1=foo a=5,b_c=6,my_field="description"
130+
my_json,my_tag_1=foo a=5,b_c=6,b_my_field="description"
131131
```
132132

133133
#### Arrays

plugins/parsers/json/parser.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"encoding/json"
66
"fmt"
77
"log"
8+
"math"
9+
"regexp"
810
"strconv"
911
"strings"
1012
"time"
@@ -13,8 +15,6 @@ import (
1315
"github.com/influxdata/telegraf/metric"
1416
"github.com/pkg/errors"
1517
"github.com/tidwall/gjson"
16-
"math"
17-
"regexp"
1818
)
1919

2020
var (
@@ -94,7 +94,6 @@ func parseUnixTimestamp(jsonValue interface{}, format string) (time.Time, error)
9494
}
9595

9696
func (p *JSONParser) parseObject(metrics []telegraf.Metric, jsonOut map[string]interface{}) ([]telegraf.Metric, error) {
97-
9897
tags := make(map[string]string)
9998
for k, v := range p.DefaultTags {
10099
tags[k] = v
@@ -108,7 +107,10 @@ func (p *JSONParser) parseObject(metrics []telegraf.Metric, jsonOut map[string]i
108107

109108
//checks if json_name_key is set
110109
if p.JSONNameKey != "" {
111-
p.MetricName = f.Fields[p.JSONNameKey].(string)
110+
switch field := f.Fields[p.JSONNameKey].(type) {
111+
case string:
112+
p.MetricName = field
113+
}
112114
}
113115

114116
//if time key is specified, set it to nTime

0 commit comments

Comments
 (0)