# How to split a mqtt json into multiple metrics

**URL:** <https://community.influxdata.com/t/how-to-split-a-mqtt-json-into-multiple-metrics/31464>\
**Category:** Telegraf\
**Tags:** json\
**Created:** [September 11, 2023, 9:45am UTC](https://community.influxdata.com/t/how-to-split-a-mqtt-json-into-multiple-metrics/31464 "2023-09-11T09:45:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonsmart9](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/simonsmart9/32/13949_2.png) [@simonsmart9](https://community.influxdata.com/u/simonsmart9)\
**Post date:** [September 11, 2023, 9:45am UTC](https://community.influxdata.com/t/how-to-split-a-mqtt-json-into-multiple-metrics/31464/1 "2023-09-11T09:45:24Z")

</div>

I have an mqtt telegraf input source containing a json string. The “message” contains two metrics separated by a comma as follows:

```auto
{"rssi":-62,"snr":9.25,"pferror":1400,"packetSize":7,"message":"104,105"}

```

I would like to split the “message” data into two metrics called “spd” and “dir” to be outputted to InfluxDB. I am aware of the processors.split function in Telegraf however do not have the experience to implement it.

Any help would be greatly appreciated.

---

<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:** [September 11, 2023, 4:33pm UTC](https://community.influxdata.com/t/how-to-split-a-mqtt-json-into-multiple-metrics/31464/2 "2023-09-11T16:33:17Z")

</div>

Hello @simonsmart9,  
Yes absolutely.  
Split as far as I understand I think you might have to use the starlark or execd processor plugin for that type of splitting.  
Here’s a similar example.  
[https://community.influxdata.com/t/how-to-split-measurement-fields-into-separate-measurements/27550/2\](https://community.influxdata.com/t/how-to-split-measurement-fields-into-separate-measurements/27550/2%5C)

---

<div class="post-metadata">

**Author:** ![simonsmart9](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/simonsmart9/32/13949_2.png) [@simonsmart9](https://community.influxdata.com/u/simonsmart9)\
**Post date:** [September 12, 2023, 8:28am UTC](https://community.influxdata.com/t/how-to-split-a-mqtt-json-into-multiple-metrics/31464/3 "2023-09-12T08:28:34Z")

</div>

Many thanks @Anaisdg  
Starlark did the trick. For anyone who is trying something similar, herewith the code.

```auto
[[inputs.mqtt_consumer]]
  servers = ["tcp://localhost:1883"]
  topics = ["higham/#"]
  connection_timeout = "30s"
  username = "mymqttusername"
  password = "mymqttpassword"
  data_format = "json_v2"

  [[inputs.mqtt_consumer.json_v2]]
     measurement_name = "higham_anemometer"
     [[inputs.mqtt_consumer.json_v2.field]]
          path = "message"
          rename = "higham_message"
          type = "string"

[[processors.starlark]]
namepass = ['higham_anemometer']
source = '''
def apply(metric):
    higham_data = metric.fields['higham_message']
    metric.fields['higham_spd'] = higham_data.split(",")[0]
    metric.fields['higham_dir'] = higham_data.split(",")[1]
    return metric
'''

```

---

<div class="post-metadata">

**Author:** ![srebhan](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/srebhan/32/8572_2.png) [@srebhan](https://community.influxdata.com/u/srebhan)\
**Post date:** [September 12, 2023, 9:37am UTC](https://community.influxdata.com/t/how-to-split-a-mqtt-json-into-multiple-metrics/31464/4 "2023-09-12T09:37:32Z")

</div>

@simonsmart9 just as a heads-up, since v1.28.0 (released yesterday) we now do have a [split processor](https://github.com/influxdata/telegraf/tree/master/plugins/processors/split) which is exactly targetting your use-case to split a metric into multiple ones…
