# Suppor needed: Mqtt Topic parsing using telegraf

**URL:** <https://community.influxdata.com/t/suppor-needed-mqtt-topic-parsing-using-telegraf/32580>\
**Category:** Telegraf\
**Tags:** telegraf, mqtt, json\
**Created:** [December 21, 2023, 1:40pm UTC](https://community.influxdata.com/t/suppor-needed-mqtt-topic-parsing-using-telegraf/32580 "2023-12-21T13:40:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kali\_muthu](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/kali_muthu/32/14789_2.png) [@kali\_muthu](https://community.influxdata.com/u/kali_muthu)\
**Post date:** [December 21, 2023, 1:40pm UTC](https://community.influxdata.com/t/suppor-needed-mqtt-topic-parsing-using-telegraf/32580/1 "2023-12-21T13:40:30Z")

</div>

This is the topic result getting from Mqtt broker, need to insert data in influxdb **" id and message " with time stamp** , pls guide how to parse this json to influx

{  
“state”: {  
“time”: 1366360.3416,  
“data”: {  
“alarms”: {  
“PS1016”: {  
“path”: 1,  
“axis\_code”: 0,  
“axis”: “”,  
“number”: 1016,  
“message”: “EOB NOT FOUND”,  
“type\_code”: 3,  
“type”: “PS”,  
“id”: “PS1016”,  
“is\_triggered”: true  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![jpowers](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/jpowers/32/8569_2.png) [@jpowers](https://community.influxdata.com/u/jpowers)\
**Post date:** [December 21, 2023, 3:48pm UTC](https://community.influxdata.com/t/suppor-needed-mqtt-topic-parsing-using-telegraf/32580/2 "2023-12-21T15:48:29Z")

</div>

Hi,

Neither ID or message are numeric values. It is unusual to create metrics without a value that can be graphs or measured over time.

This would get you all the fields:

```auto
[agent]
  debug = true
  omit_hostname = true

[[inputs.file]]
  files = ["metrics.json"]
  data_format = "xpath_json"
  xpath_allow_empty_selection = true
  xpath_native_types = true

  [[inputs.file.xpath]]
    metric_selection = "//state/data/alarms/PS1016"
    field_selection = "*"

[[outputs.file]]

```

What is that timestamp?

---

<div class="post-metadata">

**Author:** ![kali\_muthu](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/kali_muthu/32/14789_2.png) [@kali\_muthu](https://community.influxdata.com/u/kali_muthu)\
**Post date:** [December 22, 2023, 12:46am UTC](https://community.influxdata.com/t/suppor-needed-mqtt-topic-parsing-using-telegraf/32580/3 "2023-12-22T00:46:11Z")

</div>

Hi, thanks for supporting, “PS1016” value will be dynamic based on “alarms” from Mqtt topic message, This code “PS1016” and message will change based on “alarms”. i m using Mqtt \_ consumer plugin. how to store the id & message value from the dynamic Mqtt topic in influx db

Message one :  
{  
“state”: {  
“time”: 1366360.3416,  
“data”: {  
“alarms”: {  
“PS1016”: {  
“path”: 1,  
“axis\_code”: 0,  
“axis”: “”,  
“number”: 1016,  
“message”: “EOB NOT FOUND”,  
“type\_code”: 3,  
“type”: “PS”,  
“id”: “PS1016”,  
“is\_triggered”: true  
}  
}  
}  
}  
}

Message two:

{  
“state”: {  
“time”: 1366360.3416,  
“data”: {  
“alarms”: {  
“ER45”: {  
“path”: 1,  
“axis\_code”: 2,  
“axis”: “”,  
“number”: 45,  
“message”: “G code missing”,  
“type\_code”: 2,  
“type”: “ER”,  
“id”: “ER45”,  
“is\_triggered”: true  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![jpowers](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/jpowers/32/8569_2.png) [@jpowers](https://community.influxdata.com/u/jpowers)\
**Post date:** [January 2, 2024, 2:09pm UTC](https://community.influxdata.com/t/suppor-needed-mqtt-topic-parsing-using-telegraf/32580/4 "2024-01-02T14:09:09Z")

</div>

> [@jpowers](#):
>
> `metric_selection = "//state/data/alarms/PS1016"`

Think through this… you need a wild card! So how about trying:

> metric\_selection = “//state/data/alarms/\*”

---

<div class="post-metadata">

**Author:** ![kali\_muthu](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/kali_muthu/32/14789_2.png) [@kali\_muthu](https://community.influxdata.com/u/kali_muthu)\
**Post date:** [January 2, 2024, 4:15pm UTC](https://community.influxdata.com/t/suppor-needed-mqtt-topic-parsing-using-telegraf/32580/5 "2024-01-02T16:15:28Z")

</div>

I ve tried below, after adding wild card, only the second one is updated in influxdb, first one is missing, pls find the json:  
{  
“state”: {  
“time”: 266680.5801,  
“data”: {  
“alarms”: {  
“Is1016”: {  
“path”: 1,  
“axis\_code”: 5,  
“axis”: “6”,  
“number”: 1016,  
“message”: “EOB NOT FOUND”,  
“type\_code”: 3,  
“type”: “IS”,  
“id”: “IS1016”,  
“is\_triggered”: true  
},  
“js1016”: {  
“path”: 1,  
“axis\_code”: 5,  
“axis”: “6”,  
“number”: 116,  
“message”: “EOB FOUND”,  
“type\_code”: 3,  
“type”: “S”,  
“id”: “I016”,  
“is\_triggered”: true  
}  
}  
}

Config:  
[[inputs.mqtt\_consumer]]  
servers = [“tcp://127.0.0.1:1883”]  
topics = [“fanuc/mill/alarms/1”]  
qos = 0  
connection\_timeout = “30s”

```
data_format = "xpath_json"
  xpath_allow_empty_selection = true
  xpath_native_types = true

		
[[inputs.mqtt_consumer.xpath]]
  metric_selection = "//state/data/alarms/*"
  metric_name = "string('alarms')"
  timestamp = "/timestamp"
  timestamp_format = "2006-01-02T15:04:05Z"
  field_selection = "*"

```

---

<div class="post-metadata">

**Author:** ![jpowers](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/jpowers/32/8569_2.png) [@jpowers](https://community.influxdata.com/u/jpowers)\
**Post date:** [January 2, 2024, 4:36pm UTC](https://community.influxdata.com/t/suppor-needed-mqtt-topic-parsing-using-telegraf/32580/6 "2024-01-02T16:36:05Z")

</div>

You need to set some additional tags to differentiate one from the other, like type or ID. Depends on what you need to filter on.

Either defined as xpath tags or using the converter processor to convert a field to a tag:

```auto
[[processors.converter]]
  [processors.converter.fields]
    tag = ["id"]

```
