JSON parsing help

Hi. I have the following payload ;

[ {
                  "channel": 1,
                  "value": 22.54,
                  "type": "temp",
                  "unit": "c"
                },
                {
                  "channel": 2,
                  "value": 41.2,
                  "type": "rel_hum",
                  "unit": "p"
} ]

and the following telegraf config ;

    data_format = "json"
    json_string_fields = [ "type","unit" ]

       [[inputs.mqtt_consumer.topic_parsing]]
        topic = "+/+/+"
        tags = "site/room/client_id"

Problem is that only the last item of the JSON array gets to my influxdb. How can I fix that and should I use json_v2 instead of json data_format?

Thanks

Apart from the json parser configuration, that seems to be not a valid json!?
What are these values stemp, shumi under the value key?

This example should work, adjust it to your needs:

[[inputs.mqtt_consumer]]
  # the mqtt plugin config here
  # .... 
  data_format = "json_v2"
  [[inputs.mqtt_consumer.json_v2]]
    [[inputs.mqtt_consumer.json_v2.object]]
      path = "@this"
      tags = ["type"]
      [inputs.mqtt_consumer.json_v2.object.fields]
        channel = "int"
        value = "float"  # is this a float value?
        unit = "string"
1 Like

stemp , shumi are variable holding the value for temperature and humidity, variable type is float.

why do you say it’s not a valid json?

Can I keep this?

I publish to this topic format “house/room/client_id” to get the site, room and client unique id.

Because the json parser need real values, not variables.
So i assume, what you showed was a template, not the real payload.

Yes, you have to keep this, because the mqtt topic parsing is independent from the json parsing of the payload.

1 Like

Great, your help is really appreciated!

made the changes and I still get only the ‘rel_humi’ data, no ‘temp’ in influxdb


I have a basic file output that is fine, each publish generate two lines of data.

I use influxdb 1.8.10 and I check the measurements with the following query ;

use mydatabase
select * from mqtt_consumer

Then we need your full mqtt input plugin config and a sample snippet of your real json data.