diff --git a/plugins/parsers/xpath/json_document.go b/plugins/parsers/xpath/json_document.go index 5b1df08bc..8603a1ef5 100644 --- a/plugins/parsers/xpath/json_document.go +++ b/plugins/parsers/xpath/json_document.go @@ -44,7 +44,12 @@ func (d *jsonDocument) GetNodePath(node, relativeTo dataNode, sep string) string // Climb up the tree and collect the node names n := nativeNode.Parent for n != nil && n != nativeRelativeTo { - switch reflect.TypeOf(n.Parent.Value()).Kind() { + kind := reflect.Invalid + if n.Parent != nil && n.Parent.Value() != nil { + kind = reflect.TypeOf(n.Parent.Value()).Kind() + } + + switch kind { case reflect.Slice, reflect.Array: // Determine the index for array elements names = append(names, d.index(n)) diff --git a/plugins/parsers/xpath/testcases/json_array_expand/expected.out b/plugins/parsers/xpath/testcases/json_array_expand/expected.out new file mode 100644 index 000000000..bb187e2ba --- /dev/null +++ b/plugins/parsers/xpath/testcases/json_array_expand/expected.out @@ -0,0 +1 @@ +foo adr=true,applicationID="1",applicationName="Test",data="AGhCAGcA0g==",devEUI="27c0817d2aba3052",deviceName="TcsSensorNode",deviceProfileID="100ca98d-e075-4b1b-8cd3-41edbab355f5",deviceProfileName="TcsSensor",fCnt=71,fPort=2,object_humiditySensor_0=33,object_humiditySensor_1=25.5,object_temperatureSensor_0=21,object_temperatureSensor_1=19.3,rxInfo_0_gatewayID="b827ebfffeaa4582",rxInfo_0_loRaSNR=7.5,rxInfo_0_name="local",rxInfo_0_rssi=-60,rxInfo_0_uplinkID="659cbfab-3216-42fd-9f71-f2c470b7f9da",rxInfo_1_gatewayID="ca925641ce08b33e",rxInfo_1_loRaSNR=4.2,rxInfo_1_name="local",rxInfo_1_rssi=-98,rxInfo_1_uplinkID="15ca3b44-17a0-4662-82c9-aa23b40e16eb",txInfo_dr=5,txInfo_frequency=868500000,timestamp=1677099936000000000 1677099936000000000 \ No newline at end of file diff --git a/plugins/parsers/xpath/testcases/json_array_expand/telegraf.conf b/plugins/parsers/xpath/testcases/json_array_expand/telegraf.conf new file mode 100644 index 000000000..af450eb1e --- /dev/null +++ b/plugins/parsers/xpath/testcases/json_array_expand/telegraf.conf @@ -0,0 +1,12 @@ +[[inputs.file]] + files = ["./testcases/json_array_expand/test.json"] + data_format = "xpath_json" + + xpath_native_types = true + + [[inputs.file.xpath]] + field_name_expansion = true + metric_name = "'foo'" + field_selection = "descendant::*" + timestamp = "//timestamp" + timestamp_format = "unix_ns" diff --git a/plugins/parsers/xpath/testcases/json_array_expand/test.json b/plugins/parsers/xpath/testcases/json_array_expand/test.json new file mode 100644 index 000000000..793bb5c11 --- /dev/null +++ b/plugins/parsers/xpath/testcases/json_array_expand/test.json @@ -0,0 +1,43 @@ +{ + "applicationID":"1", + "applicationName":"Test", + "deviceName":"TcsSensorNode", + "deviceProfileName":"TcsSensor", + "deviceProfileID":"100ca98d-e075-4b1b-8cd3-41edbab355f5", + "devEUI":"27c0817d2aba3052", + "timestamp": 1677099936000000000, + "rxInfo":[ + { + "gatewayID":"b827ebfffeaa4582", + "uplinkID":"659cbfab-3216-42fd-9f71-f2c470b7f9da", + "name":"local", + "rssi":-60, + "loRaSNR":7.5 + }, + { + "gatewayID":"ca925641ce08b33e", + "uplinkID":"15ca3b44-17a0-4662-82c9-aa23b40e16eb", + "name":"local", + "rssi":-98, + "loRaSNR":4.2 + } + ], + "txInfo":{ + "frequency":868500000, + "dr":5 + }, + "adr":true, + "fCnt":71, + "fPort":2, + "data":"AGhCAGcA0g==", + "object":{ + "humiditySensor":{ + "0":33, + "1":25.5 + }, + "temperatureSensor":{ + "0":21, + "1":19.3 + } + } +} \ No newline at end of file