diff --git a/plugins/parsers/json/parser.go b/plugins/parsers/json/parser.go index a651ae534..7e138e33a 100644 --- a/plugins/parsers/json/parser.go +++ b/plugins/parsers/json/parser.go @@ -193,10 +193,13 @@ func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) { if p.query != "" { result := gjson.GetBytes(buf, p.query) buf = []byte(result.Raw) - if !result.IsArray() && !result.IsObject() { - err := fmt.Errorf("E! Query path must lead to a JSON object or array of objects, but lead to: %v", result.Type) + if !result.IsArray() && !result.IsObject() && result.Type != gjson.Null { + err := fmt.Errorf("E! Query path must lead to a JSON object, array of objects or null, but lead to: %v", result.Type) return nil, err } + if result.Type == gjson.Null { + return nil, nil + } } buf = bytes.TrimSpace(buf) @@ -217,6 +220,8 @@ func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) { return p.parseObject(v, timestamp) case []interface{}: return p.parseArray(v, timestamp) + case nil: + return nil, nil default: return nil, ErrWrongType } diff --git a/plugins/parsers/json/parser_test.go b/plugins/parsers/json/parser_test.go index 9abe853ec..1010d7971 100644 --- a/plugins/parsers/json/parser_test.go +++ b/plugins/parsers/json/parser_test.go @@ -892,6 +892,18 @@ func TestParse(t *testing.T) { input: []byte(`[]`), expected: []telegraf.Metric{}, }, + { + name: "parse null", + config: &Config{}, + input: []byte(`null`), + expected: []telegraf.Metric{}, + }, + { + name: "parse null with query", + config: &Config{Query: "result.data"}, + input: []byte(`{"error":null,"result":{"data":null,"items_per_page":10,"total_items":0,"total_pages":0}}`), + expected: []telegraf.Metric{}, + }, { name: "parse simple array", config: &Config{