# Json\_v2 parsing multiple objects in same payload results in ignored objects

**URL:** <https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799>\
**Category:** Telegraf\
**Tags:** json\
**Created:** [July 18, 2022, 3:31pm UTC](https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799 "2022-07-18T15:31:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![whchung](https://avatars.discourse-cdn.com/v4/letter/w/13edae/32.png) [@whchung](https://community.influxdata.com/u/whchung)\
**Post date:** [July 18, 2022, 3:31pm UTC](https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799/1 "2022-07-18T15:31:10Z")

</div>

Hello!

I’m trying to parse a JSON payload using telegraph json\_v2. I’d like to use multiple [[inputs.xxx.json\_v2.object]] sections to parse different objects in the payload. However, only the first object is getting parsed and written to InfluxDB. If I add a json\_v2.object section for the second object, then there are no error messages but nothing gets written. Same thing if I just have one json\_v2.object section for the second object only. Any ideas why the parser seems to be ignoring anything beyond the first object?

My input JSON (made generic to avoid sensitive details):

```auto
{
  "object1": {
    "firstname": "John",
    "lastname": "Smith",
    "address": 10,
    "type": "residential"
  },
  "object2": {
    "lat": "0",
    "lon": "1"
  },
  "object3": {
    "Deliver": true,
    "Type": "0x1",
    "Version": "0x02",
    "location1": {
      "temp": "25",
      "minTemp": "20",
      "maxTemp": "30"
    },
    "location2": {
      "temp": "25",
      "minTemp": "20",
      "maxTemp": "30"
    },
    "AvgTemp": "20",
    "CurrentTemp": "21"
  }
}

```

My telegraf config snippet:

```auto
[[inputs.xxx.json_v2.object]]
path = "object1"
tags = ["firstname", "last name", "type"]

[[inputs.xxx.json_v2.object]]
path = "object2"
tags = ["lat", "lon"]

```

telegraf is happy to parse object1 and write it to InfluxDB. But object2 never gets parsed as far as I can tell. I thought maybe only one json\_v2.object section was allowed? However, even if I just include the object definition for object2, it results in nothing written to InfluxDB.

Thank you for any suggestions about what may be wrong!

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://community.influxdata.com/u/phill)\
**Post date:** [July 18, 2022, 6:33pm UTC](https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799/2 "2022-07-18T18:33:47Z")

</div>

Hi @whchung - Welcome to the Community Forum!

Check the indentation. The example below was copied from working code in my conf file.

```auto
 data_format = "json_v2"

  [[inputs.http.json_v2]]
    measurement_name = "something"

  # TOML subtables for JSON V2 parser
    [[inputs.http.json_v2.object]]
      path = "myPath"
      included_keys = ["myKey"]
      tags = ["myTag"]

    [[inputs.http.json_v2.object]]
      path = "anotherPath"
      included_keys = ["anotherKey"]

```

There is also a [JSON Playground](https://gjson.dev/) where you can verify that your path (the part inside the quotes) is picking off the correct name-value pairs.

Phill

---

<div class="post-metadata">

**Author:** ![whchung](https://avatars.discourse-cdn.com/v4/letter/w/13edae/32.png) [@whchung](https://community.influxdata.com/u/whchung)\
**Post date:** [July 18, 2022, 6:41pm UTC](https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799/3 "2022-07-18T18:41:29Z")

</div>

Phill,

Thank you for your response! Yes, I used the JSON Playground to verify my paths.

Your example shows more than one json\_v2.object section with more than one path. So it looks like using more than one json\_v2.object section should work 👍. I’ll try this again, following your example with regard to indentation, now that I know indentation matters.

whchung

---

<div class="post-metadata">

**Author:** ![whchung](https://avatars.discourse-cdn.com/v4/letter/w/13edae/32.png) [@whchung](https://community.influxdata.com/u/whchung)\
**Post date:** [July 18, 2022, 8:30pm UTC](https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799/4 "2022-07-18T20:30:04Z")

</div>

Can each of the json\_v2.object sections have a different tag definition?

Also, what does the indentation look like if I’m using mqtt\_consumer with additional subsections, such as topic parsing? I tried the snippet below using the indentation from your example with no luck. The second json\_v2.object section is ignored; I get no “anotherMyTag” despite the path “anotherPath” being correct.

```auto
data_format = "json_v2"
  [[inputs.mqtt_consumer.topic_parsing]]
    topic = "+/+/+"

  [[inputs.mqtt_consumer.json_v2]]
    measurement_name = "something"

  # TOML subtables for JSON V2 parser
    [[inputs.http.json_v2.object]]
      path = "myPath"
      included_keys = ["myKey"]
      tags = ["myTag"]

    [[inputs.http.json_v2.object]]
      path = "anotherPath"
      tags = ["anotherMyTag"]

```

---

<div class="post-metadata">

**Author:** ![whchung](https://avatars.discourse-cdn.com/v4/letter/w/13edae/32.png) [@whchung](https://community.influxdata.com/u/whchung)\
**Post date:** [July 18, 2022, 8:42pm UTC](https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799/5 "2022-07-18T20:42:44Z")

</div>

Interestingly, using a single json\_v2.object definition works. At least I get tags for both objects. I needed to specify the tags using a path prefix with \_, not ., as the separator. I’m now wondering again if using two json\_v2.object definitions is a problem, especially with the mqtt\_consumer plugin vs http?

```auto
[[inputs.mqtt_consumer.json_v2.object]]
  path = "@this"
  tags = ["myPath_myKey", "anotherPath_anotherKey"]

```

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://community.influxdata.com/u/phill)\
**Post date:** [July 18, 2022, 8:52pm UTC](https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799/6 "2022-07-18T20:52:57Z")

</div>

@whchung

> [@whchung](#):
>
> ```auto
> [[inputs.mqtt_consumer.json_v2]]
> measurement_name = "something"
> 
> # TOML subtables for JSON V2 parser
> [[inputs.http.json_v2.object]]
> 
> ```

Be careful - This is mixing **inputs.mqtt** with **inputs.http** which won’t work. All TOML subtables below the initial `[[inputs.whatever]]` plugin declaration must start with the same `[[inputs.whatever. . . .]]`

> [@whchung](#):
>
> Can each of the json\_v2.object sections have a different tag definition?

Yes

---

<div class="post-metadata">

**Author:** ![whchung](https://avatars.discourse-cdn.com/v4/letter/w/13edae/32.png) [@whchung](https://community.influxdata.com/u/whchung)\
**Post date:** [July 18, 2022, 9:00pm UTC](https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799/7 "2022-07-18T21:00:41Z")

</div>

@phill Sorry, I’m using inputs.mqtt\_consumer with everything, with no http. The mixing in my example was a copy/paste error 😊. So I’m really using something like the snippet below. I never get tag “anotherMyTag” to show up unless I only use one `[[inputs.mqtt_consumer.json_v2]]` section and then define the tags using the object path prefix as I showed above.

```auto
data_format = "json_v2"
  [[inputs.mqtt_consumer.topic_parsing]]
    topic = "+/+/+"

  [[inputs.mqtt_consumer.json_v2]]
    measurement_name = "something"

  # TOML subtables for JSON V2 parser
    [[inputs.mqtt_consumer.json_v2.object]]
      path = "myPath"
      included_keys = ["myKey"]
      tags = ["myTag"]

    [[inputs.mqtt_consumer.json_v2.object]]
      path = "anotherPath"
      tags = ["anotherMyTag"]

```

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://community.influxdata.com/u/phill)\
**Post date:** [July 18, 2022, 11:43pm UTC](https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799/8 "2022-07-18T23:43:45Z")

</div>

@whchung  
Take a look at this post. If you can’t get everything to work in 1 plugin, you can split it up into more. Also `mqtt_consumer` parsing is discussed in more detail.

> [@MQTT plugin not writing data to influxdb](https://community.influxdata.com/t/mqtt-plugin-not-writing-data-to-influxdb/25632/4):
>
> Hi @jmo @phill, So it is more than acceptable to have two or even more of the same plugin type within your config. In some cases we even encourage it to spread the load with large datasets. Can I propose a config reshuffle: [[inputs.mqtt\_consumer]] servers = ["tcp://192.168.50.36:1883"] topics = ["perfscore"] topic\_tag = "topic" qos = 0 connection\_timeout = "30s" max\_undelivered\_messages = 1000 persistent\_session = false data\_format = "value" data\_type = "i…

---

<div class="post-metadata">

**Author:** ![whchung](https://avatars.discourse-cdn.com/v4/letter/w/13edae/32.png) [@whchung](https://community.influxdata.com/u/whchung)\
**Post date:** [July 19, 2022, 1:31am UTC](https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799/9 "2022-07-19T01:31:06Z")

</div>

@phill I appreciate the pointer. I looked at the post you referred to. It’s using two different MQTT brokers, so I can understand why using two different mqtt\_consumer plugins makes sense.

The post also uses mqtt\_consumer.topic\_parsing to generate tags, not the json\_v2 parser. It dumps the entire payload as keys using `path = "@this"`. I was hoping to use the json\_v2 parser to generate both tags and keys from the different JSON objects in my payload by referring to the different objects using different paths. You gave an example that shows this works with the http plugin . Did you try this with the mqtt\_consumer plugin? Given the other post you referred to, I’m wondering if the json\_v2 parser might behave differently with mqtt\_consumer vs http.

Thank you!

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://community.influxdata.com/u/phill)\
**Post date:** [July 19, 2022, 1:02pm UTC](https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799/10 "2022-07-19T13:02:04Z")

</div>

@whchung I use mqtt to relay sensor data to Home Assistant but I haven’t used the `[[inputs.mqtt_consumer]]` plugin. So there’s the JSON payload inside an mqtt wrapper (the topic) and I assume the `topic_parsing` is required to unwrap and get to the payload.

I think you could remove the `tags = "_/_/_/network/_/device/_"` statement from the `[[inputs.mqtt_consumer.topic_parsing]]` section and put the tags you need in the JSON parser. If this doesn’t work, there’s a tag rename (see below).

> [@whchung](#):
>
> It dumps the entire payload as keys using `path = "@this"`

Yes, `path = "@this"` selects the entire payload for that example, though you can change this to pick off specific name-value pairs, if desired (see below).

I did not include all the details of my http JSON parser earlier, but renaming tags is possible. Note in this example I have an array of JSON objects and use pattern matching to drill down into the array. Then the `@this` refers to the JSON fragment that remains after the drill down.

```auto
    [[inputs.http.json_v2.object]]
      path = "devices.#(SpecificDeviceMatching)#.@this"
      included_keys = ["MyKey"]
      tags = ["MyTag"]
      [inputs.http.json_v2.object.renames]
        MyTag = "MyNewTag"
        MyKey = "MyNewKey"
 

```

---

<div class="post-metadata">

**Author:** ![whchung](https://avatars.discourse-cdn.com/v4/letter/w/13edae/32.png) [@whchung](https://community.influxdata.com/u/whchung)\
**Post date:** [July 22, 2022, 3:58pm UTC](https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799/11 "2022-07-22T15:58:37Z")

</div>

I gave up trying to define multiple `[[inputs.mqtt_consumer.json_v2.object]]` sections. It really does look like only one will work.

To define fields, I just wrote out multiple `[[inputs.mqtt\_consumer.json\_v2.field]] sections to define each of the fields I wanted. This was not as elegant as being to specify a path in an object definition, but it seems to work.

Thank you for your help @phill!

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://community.influxdata.com/u/phill)\
**Post date:** [July 22, 2022, 5:43pm UTC](https://community.influxdata.com/t/json-v2-parsing-multiple-objects-in-same-payload-results-in-ignored-objects/25799/12 "2022-07-22T17:43:31Z")

</div>

@whchung Sometimes brute force wins over elegance. Glad to hear it’s working. Good luck with your project!
