Just for someone who is struggling on array of arrays to read tags and fields, here is one way to do it.
Goal is to get line protocol output for named tags with energy as a field value.
And if someone is wandering the MQTT topic comes from a go-e EV Charger which has a nice open API to read.
What I get from an MQTT topic is:
[{
"name": "Device name 1",
"energy": 0,
"cardId": true
}, {
"name": "Device name 2",
"energy": 0,
"cardId": true
}, {
"name": "n/a",
"energy": 0,
"cardId": false
}, {
"name": "n/a",
"energy": 0,
"cardId": false
}, {
"name": "n/a",
"energy": 0,
"cardId": false
}]
Output includes up to 10 nodes, but I shortened it for the purpose of example.
I’ve setup an mqtt_consumer configuration as
[[inputs.mqtt_consumer]]
servers = ["tcp://localhost:1883"]
topics = [ "goeCharger/+/cards" ]
data_format = "json_v2"
[[inputs.mqtt_consumer.topic_parsing]]
topic = "goeCharger/+/+"
measurement = "measurement/_/_"
tags = "_/chargerid/_"
[[inputs.mqtt_consumer.json_v2]]
[[inputs.mqtt_consumer.json_v2.object]]
path = "@this.#(cardId=true)#"
tags = ["name"]
include_keys = ["name", "energy"]
exclude_keys = ["cardId"]
[inputs.file.json_v2.object.fields]
energy = "float"
cardId = "boolean"
The GJSON filter path @this.#(cardId=true)#
should match and leave just the nodes with cardId=true
as tested with https://gjson.dev :
[{
"name": "Device name 1",
"energy": 0,
"cardId": true
},{
"name": "Device name 2",
"energy": 0,
"cardId": true
}]
This results for output as
goeCharger,chargerid=201397,name="Device name 1" cardId=true,energy=0 1664257051533353589
goeCharger,chargerid=201397,name="Device name 2" cardId=true,energy=0 1664257051533353589
GJSON filters with JSON objects are nice and powerful to leave out unnecessary data to be pushed the the database.