Fix json_v2 parser to handle nested objects in arrays properly (#9479)

This commit is contained in:
Sebastian Spaink 2021-07-08 13:05:41 -05:00 committed by GitHub
parent b3492fcfa0
commit 1b20680e37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 1 deletions

View File

@ -381,6 +381,7 @@ func (p *Parser) processObjects(objects []JSONObject, input []byte) ([]telegraf.
// If the object has multiple array's as elements it won't comine those, they will remain separate metrics
func (p *Parser) combineObject(result MetricNode) ([]MetricNode, error) {
var results []MetricNode
var combineObjectResult []MetricNode
if result.IsArray() || result.IsObject() {
var err error
var prevArray bool
@ -437,7 +438,7 @@ func (p *Parser) combineObject(result MetricNode) ([]MetricNode, error) {
arrayNode.Tag = tag
if val.IsObject() {
prevArray = false
_, err = p.combineObject(arrayNode)
combineObjectResult, err = p.combineObject(arrayNode)
if err != nil {
return false
}
@ -477,6 +478,12 @@ func (p *Parser) combineObject(result MetricNode) ([]MetricNode, error) {
}
}
if len(results) == 0 {
// If the results are empty, use the results of the call to combine object
// This happens with nested objects in array's, see the test array_of_objects
results = combineObjectResult
}
return results, nil
}

View File

@ -21,6 +21,10 @@ func TestData(t *testing.T) {
name string
test string
}{
{
name: "Test having an array of objects",
test: "array_of_objects",
},
{
name: "Test using just fields and tags",
test: "fields_and_tags",

View File

@ -0,0 +1,2 @@
file properties_mag=5.17
file properties_mag=6.2

View File

@ -0,0 +1,14 @@
{
"features": [
{
"properties": {
"mag": 5.17
}
},
{
"properties": {
"mag": 6.2
}
}
]
}

View File

@ -0,0 +1,9 @@
# Example taken from: https://github.com/influxdata/telegraf/issues/5940
[[inputs.file]]
files = ["./testdata/array_of_objects/input.json"]
data_format = "json_v2"
[[inputs.file.json_v2]]
[[inputs.file.json_v2.object]]
path = "features"