# JSON parsing help

**URL:** <https://community.influxdata.com/t/json-parsing-help/24019>\
**Category:** Telegraf\
**Tags:** telegraf, json\
**Created:** [February 25, 2022, 1:12pm UTC](https://community.influxdata.com/t/json-parsing-help/24019 "2022-02-25T13:12:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yannickg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/yannickg/32/9561_2.png) [@yannickg](https://community.influxdata.com/u/yannickg)\
**Post date:** [February 25, 2022, 1:12pm UTC](https://community.influxdata.com/t/json-parsing-help/24019/1 "2022-02-25T13:12:15Z")

</div>

Hi. I have the following payload ;

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

```

and the following telegraf config ;

```auto
    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

---

<div class="post-metadata">

**Author:** ![Franky1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/franky1/32/6666_2.png) [@Franky1](https://community.influxdata.com/u/Franky1)\
**Post date:** [February 25, 2022, 1:26pm UTC](https://community.influxdata.com/t/json-parsing-help/24019/2 "2022-02-25T13:26:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Franky1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/franky1/32/6666_2.png) [@Franky1](https://community.influxdata.com/u/Franky1)\
**Post date:** [February 25, 2022, 1:36pm UTC](https://community.influxdata.com/t/json-parsing-help/24019/3 "2022-02-25T13:36:42Z")

</div>

This example should work, adjust it to your needs:

```auto
[[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"

```

---

<div class="post-metadata">

**Author:** ![yannickg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/yannickg/32/9561_2.png) [@yannickg](https://community.influxdata.com/u/yannickg)\
**Post date:** [February 25, 2022, 1:41pm UTC](https://community.influxdata.com/t/json-parsing-help/24019/4 "2022-02-25T13:41:54Z")

</div>

`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?

---

<div class="post-metadata">

**Author:** ![yannickg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/yannickg/32/9561_2.png) [@yannickg](https://community.influxdata.com/u/yannickg)\
**Post date:** [February 25, 2022, 1:45pm UTC](https://community.influxdata.com/t/json-parsing-help/24019/5 "2022-02-25T13:45:09Z")

</div>

> [@yannickg](#):
>
> ```auto
> [[inputs.mqtt_consumer.topic_parsing]]
> topic = "+/+/+"
> tags = "site/room/client_id"
> 
> ```

Can I keep this?

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

---

<div class="post-metadata">

**Author:** ![Franky1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/franky1/32/6666_2.png) [@Franky1](https://community.influxdata.com/u/Franky1)\
**Post date:** [February 25, 2022, 1:50pm UTC](https://community.influxdata.com/t/json-parsing-help/24019/6 "2022-02-25T13:50:38Z")

</div>

> [@yannickg](#):
>
> why do you say it’s not a valid json?

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

> [@yannickg](#):
>
> Can I keep this?

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

---

<div class="post-metadata">

**Author:** ![yannickg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/yannickg/32/9561_2.png) [@yannickg](https://community.influxdata.com/u/yannickg)\
**Post date:** [February 25, 2022, 1:52pm UTC](https://community.influxdata.com/t/json-parsing-help/24019/7 "2022-02-25T13:52:26Z")

</div>

Great, your help is really appreciated!

---

<div class="post-metadata">

**Author:** ![yannickg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/yannickg/32/9561_2.png) [@yannickg](https://community.influxdata.com/u/yannickg)\
**Post date:** [February 25, 2022, 3:35pm UTC](https://community.influxdata.com/t/json-parsing-help/24019/8 "2022-02-25T15:35:36Z")

</div>

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 ;

```auto
use mydatabase
select * from mqtt_consumer

```

---

<div class="post-metadata">

**Author:** ![Franky1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/franky1/32/6666_2.png) [@Franky1](https://community.influxdata.com/u/Franky1)\
**Post date:** [February 26, 2022, 7:47am UTC](https://community.influxdata.com/t/json-parsing-help/24019/9 "2022-02-26T07:47:59Z")

</div>

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