# MQTT Telegraf JSON - trailing NULL character - payload parsing problem

**URL:** <https://community.influxdata.com/t/mqtt-telegraf-json-trailing-null-character-payload-parsing-problem/29111>\
**Category:** Telegraf\
**Created:** [March 18, 2023, 4:27pm UTC](https://community.influxdata.com/t/mqtt-telegraf-json-trailing-null-character-payload-parsing-problem/29111 "2023-03-18T16:27:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom999](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@Tom999](https://community.influxdata.com/u/Tom999)\
**Post date:** [March 18, 2023, 4:27pm UTC](https://community.influxdata.com/t/mqtt-telegraf-json-trailing-null-character-payload-parsing-problem/29111/1 "2023-03-18T16:27:59Z")

</div>

Hello All,

following input quasi JSON:  
‘{“TimeStamp”:1679156093057,“WorkCenter”:“MyPlant1”,“PlantCode”:“4183”,“LineName”:“Line001”,“MachineName”:“AssemblerNo1”,“StationName”:“10N1”,“DeviceName”:“HardStop”,“TagName”:“ExtCmd\_T”,“Value”:50,“Anomaly”:0,“AvgWindow”:50.0,“StdWindow”:0.0} NULL’

as you can see last character in this string is NULL which is produced by PLC. I can’t get rid of it.

Now how can I convert this in fact non-JSON into valid line protocol for InfluxDB ver2.0?

How to parse tags and fields from payload (not topic) os this JSON?  
How to create measurement name as hard-coded name (not parsed from topic or payload) ?

I tried many configurations and read all topics in internet.  
There is no clear documentation with examples how to handle such issue.

What I found is:

> <https://github.com/influxdata/telegraf/issues/7536>
>
> Hi everyone,
> 
> I’m working on some project, where I need to collect data to inf…luxdb using mqtt input plugin in telegraf. But I’m facing strange behaviour of mqtt input. I’m sending data on specific topic using JSON string (i.e. “{value”:15}", but MQTT producer, which I’m using adds, null (’\\x00’) char at the end of message. It looks like below in preview using MQTTBox client:
> !\[14eeee37bcfdb5e2943c54c0281cae077d6ebade\](https://user-images.githubusercontent.com/65446967/82209574-c2337c00-990d-11ea-88af-1e5f69277588.png)
> 
> 
> 
> First message was sent from MQTT producer and it has lenght of 41 bytes, while second I sent from regular MQTT client - MQTTBox - length of the second message is 40 bytes - null character at the end is missing. First message causes error in telegraf, while second is correctly wrote to database:
> 
> \`\`\`
> 2020-05-16T14:03:14Z E! \[inputs.mqtt\_consumer\] Error in plugin: invalid character '\\x00' after top-level value
> 2020-05-16T14:03:15Z E! \[inputs.mqtt\_consumer\] Error in plugin: invalid character '\\x00' after top-level value
> \`\`\`
> My current telegraf config file looks like this (I have to use multiple MQTT inputs to multiple Influx databases, but this not a problem):
> 
> \`\`\`
> \[\[outputs.influxdb\]\]
> urls = \["http://localhost:8086"\]
> database = "telegraf\_metrics\_01"
> retention\_policy = ""
> write\_consistency = "any"
> timeout = "37s"
> tagexclude = \["destinationdb"\]
> \[outputs.influxdb.tagpass\]
> destinationdb = \["db01"\]
> 
> \[\[inputs.mqtt\_consumer\]\]
> servers = \["tcp://localhost:1883"\]
> qos = 0
> topics = \[
> "test/maszyna1/#"
> \]
> data\_format = "json"
> 
> \[inputs.mqtt\_consumer.tags\]
> destinationdb = "db01"
> \`\`\`
> Do you have any idea, how to configure telegraf to (maybe) ignore those null characters, or cut this one byte using some processor plugin? I read some about proccessors.string.trim\_right plugin, but as I tested - it only works, when input data are correct, so It won't be helpful in this case. 
> 
> It’s also important that, when I’m using “value” data\_format of mqtt input, instead of “json” - telegraf works fine with this mqtt producer, even at the end of simple value also null character appears. 
> \*\*So maybe plugin (or even parser) should ignore those characters in payload (i.e. whitespaces, nulls) by default?\*\* Anyways - I have to use json format to pass multiple parameters to influx at once. 
> 
> I will appreciate any help
> Have a good day!

So probably parsing the non-Json from string to valid JSON is needed. But I use InfluxDB \>=2.0  
I used also docker.

Here is my telegraf.conf:

```auto
[agent]
  interval = "1s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  hostname = ""
  omit_hostname = true

##############################################################################
[[inputs.mqtt_consumer]]
  servers = ["tcp://MQTT_Broker:1883"]
  topics = ["PPS_L1/AssemblerNo1/10N1/HardStop/ExtCmd_T"]
  username = "mqttbroker"
  password = "LoopEdge.123"

  #data_format = "json"
  ##data_format = "influx"
  data_format = "value"
  data_type = "string"
  #data_format = "json_v2"
  topic_tag = ""

  [[inputs.mqtt_consumer.json_v2]]
    measurement_name = "measurement1"
    timestamp_path = "TimeStamp"
    timestamp_format = "unix"
    [[inputs.mqtt_consumer.json_v2.tag]]
      path = "LineName"

    [[inputs.mqtt_consumer.json_v2.field]]
      path = "Value"
      type = "float"

    [[inputs.mqtt_consumer.json_v2.field]]
      path = "AvgWindow"
      type = "float"

###############################################################################
[[processors.parser]]
 parse_fields = ["Value", "Anomaly", "AvgWindow"]
 parse_tags = ["LineName", "MachineName"]

 merge = "override"
 data_format = "json"

###############################################################################
[[outputs.influxdb_v2]]
  urls = ["http://InfluxDB:8086"]
  token = "firToJO9jjn0USZfpmAg5RfU5-R5BCKYjDFHNL8j3oI3VmVAjEQxLmPRq3uGzQw5w6t8PuBGy-A5KRzjv_7EBA=="
  organization = "Company"
  namepass = ["measurement1"]
  bucket = "BUCKET_TEST"

```

So please, please any hint and clear answers with vallid telegraf.conf file are appreciated.

Regards  
Tom

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [March 19, 2023, 9:16pm UTC](https://community.influxdata.com/t/mqtt-telegraf-json-trailing-null-character-payload-parsing-problem/29111/2 "2023-03-19T21:16:32Z")

</div>

Hello @Tom999,  
I like to use the `--test` flag to see what the resulting line protocol looks like and then fix my config based off of that.  
As well as setting `debug=true` in the agent portion of the config.

> **[Telegraf commands and flags | Telegraf 1.21 Documentation](https://docs.influxdata.com/telegraf/v1.21/administration/commands/)**
>
> The telegraf command starts and runs all the processes necessary for Telegraf to function.

Can you please use that and share the output?

---

<div class="post-metadata">

**Author:** ![Tom999](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@Tom999](https://community.influxdata.com/u/Tom999)\
**Post date:** [March 23, 2023, 10:21am UTC](https://community.influxdata.com/t/mqtt-telegraf-json-trailing-null-character-payload-parsing-problem/29111/3 "2023-03-23T10:21:36Z")

</div>

OK, I managed to fix it on the PLC level to send valid JSON.  
Thank you.
