Make JSON format compatible with nulls (#9110)
This commit is contained in:
parent
e29bca7419
commit
4336dae3b5
|
|
@ -193,10 +193,13 @@ func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
|
||||||
if p.query != "" {
|
if p.query != "" {
|
||||||
result := gjson.GetBytes(buf, p.query)
|
result := gjson.GetBytes(buf, p.query)
|
||||||
buf = []byte(result.Raw)
|
buf = []byte(result.Raw)
|
||||||
if !result.IsArray() && !result.IsObject() {
|
if !result.IsArray() && !result.IsObject() && result.Type != gjson.Null {
|
||||||
err := fmt.Errorf("E! Query path must lead to a JSON object or array of objects, but lead to: %v", result.Type)
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if result.Type == gjson.Null {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = bytes.TrimSpace(buf)
|
buf = bytes.TrimSpace(buf)
|
||||||
|
|
@ -217,6 +220,8 @@ func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
|
||||||
return p.parseObject(v, timestamp)
|
return p.parseObject(v, timestamp)
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
return p.parseArray(v, timestamp)
|
return p.parseArray(v, timestamp)
|
||||||
|
case nil:
|
||||||
|
return nil, nil
|
||||||
default:
|
default:
|
||||||
return nil, ErrWrongType
|
return nil, ErrWrongType
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -892,6 +892,18 @@ func TestParse(t *testing.T) {
|
||||||
input: []byte(`[]`),
|
input: []byte(`[]`),
|
||||||
expected: []telegraf.Metric{},
|
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",
|
name: "parse simple array",
|
||||||
config: &Config{
|
config: &Config{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue