# Json\_v2 array parsing

**URL:** <https://community.influxdata.com/t/json-v2-array-parsing/28529>\
**Category:** Telegraf\
**Tags:** telegraf, json\
**Created:** [February 10, 2023, 7:58am UTC](https://community.influxdata.com/t/json-v2-array-parsing/28529 "2023-02-10T07:58:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shilga](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/shilga/32/12622_2.png) [@shilga](https://community.influxdata.com/u/shilga)\
**Post date:** [February 10, 2023, 7:58am UTC](https://community.influxdata.com/t/json-v2-array-parsing/28529/1 "2023-02-10T07:58:56Z")

</div>

Hi,

I’m trying to parse an array with several measurements but I’m unable to solve it. I tried to find a solution but it seems that json\_v2 is unable to do what I want. I’m here to ask if I oversaw a feature or if I have to find a completely different solution to fetch my data.

My JSON:

```auto
{
   "applicationID":"1",
   "applicationName":"Test",
   "deviceName":"TcsSensorNode",
   "deviceProfileName":"TcsSensor",
   "deviceProfileID":"100ca98d-e075-4b1b-8cd3-41edbab355f5",
   "devEUI":"27c0817d2aba3052",
   "rxInfo":[
      {
         "gatewayID":"b827ebfffeaa4582",
         "uplinkID":"659cbfab-3216-42fd-9f71-f2c470b7f9da",
         "name":"local",
         "rssi":-60,
         "loRaSNR":7.5
      },
      {
         "gatewayID":"ca925641ce08b33e",
         "uplinkID":"15ca3b44-17a0-4662-82c9-aa23b40e16eb",
         "name":"local",
         "rssi":-98,
         "loRaSNR":4.2
      }
   ],
   "txInfo":{
      "frequency":868500000,
      "dr":5
   },
   "adr":true,
   "fCnt":71,
   "fPort":2,
   "data":"AGhCAGcA0g==",
   "object":{
      "humiditySensor":{
         "0":33,
         "1":25.5
      },
      "temperatureSensor":{
         "0":21,
         "1":19.3
      }
   }
}

```

The problem is that that in the `rxInfo` array only the second values are used. Solutions I found through googling was that the array might need to be flattened first. But I can’t change the format of this. If I write my own application to receive the values I don’t need Telegraf anymore I can directly push to the database.

The current config:

```auto
  [[inputs.mqtt_consumer.json_v2]]
    measurement_name = "sensors_test2"

    [[inputs.mqtt_consumer.json_v2.object]]
      path = "rxInfo"
      included_keys = ["rssi", "loRaSNR"]
      [inputs.mqtt_consumer.json_v2.object.fields]
        rssi = "float"
        loRaSNR = "float"
    [[inputs.mqtt_consumer.json_v2.object]]
      path = "txInfo"

```

Hope somebody knows a solution. Thank you 🙂

---

<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:** [February 10, 2023, 1:40pm UTC](https://community.influxdata.com/t/json-v2-array-parsing/28529/2 "2023-02-10T13:40:31Z")

</div>

> [@shilga](#):
>
> The problem is that that in the `rxInfo` array only the second values are used.

This is most likely because you have no tags that differentiate the first and second array items. Internally your current config should create metrics like:

```s
metric rssi=-6,loRaSNR=7.5
metric rssi=-98,IoRaSNR=4.2

```

There is nothing in there to differentiate between the two and so the last one will win.

---

<div class="post-metadata">

**Author:** ![shilga](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/shilga/32/12622_2.png) [@shilga](https://community.influxdata.com/u/shilga)\
**Post date:** [February 10, 2023, 2:02pm UTC](https://community.influxdata.com/t/json-v2-array-parsing/28529/3 "2023-02-10T14:02:06Z")

</div>

I would have expected that it would create something like `0_rssi` and `1_rssi`. I’m not sure if tags are a good solution.

Anyway I would need a tag like `rxInfo` with the value `0` and `1`. So the iterator should become the value. I also don’t know how many items are in the array. It depends on how many gateways received the message. I can’t find info on how I can use the array iterator in any way in the documentation.

That is my main issue. The documentation just does not apply to the usecase here. Arrays are apparently not thought about.

---

<div class="post-metadata">

**Author:** ![shilga](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/shilga/32/12622_2.png) [@shilga](https://community.influxdata.com/u/shilga)\
**Post date:** [February 14, 2023, 11:21am UTC](https://community.influxdata.com/t/json-v2-array-parsing/28529/4 "2023-02-14T11:21:23Z")

</div>

Hi,  
thanks for your suggestion. Really appreciated.  
I tied to put the iterator of the array into a tag, but I can’t find any info on how to do that. As you suggested that, do you know the approach to do so?

---

<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:** [February 14, 2023, 2:07pm UTC](https://community.influxdata.com/t/json-v2-array-parsing/28529/5 "2023-02-14T14:07:10Z")

</div>

> [@shilga](#):
>
> As you suggested that, do you know the approach to do so?

I do not, @srebhan are you aware of any way to go through a JSON array and create key’s with the index as a part of it?

---

<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:** [February 22, 2023, 9:29pm UTC](https://community.influxdata.com/t/json-v2-array-parsing/28529/6 "2023-02-22T21:29:16Z")

</div>

@shilga while playing with your example I found a panic in the xpath parser, so thanks for bringing up this challenge! 😉 Anyway, you can use the binary from [fix(parsers.xpath): Fix panic for JSON name expansion by srebhan · Pull Request #12724 · influxdata/telegraf · GitHub](https://github.com/influxdata/telegraf/pull/12724) with

```plaintext
[[inputs.file]]
  files = ["test_configs/jsontest_forum_shilga.json"]
  data_format = "xpath_json"
  xpath_native_types = true

  [[inputs.file.xpath]]
    metric_name = "'foo'"
    field_name_expansion = true
    field_selection = "descendant::*"

```

you will get

```plaintext
> foo adr=true,applicationID="1",applicationName="Test",data="AGhCAGcA0g==",devEUI="27c0817d2aba3052",deviceName="TcsSensorNode",deviceProfileID="100ca98d-e075-4b1b-8cd3-41edbab355f5",deviceProfileName="TcsSensor",fCnt=71,fPort=2,object_humiditySensor_0=33,object_humiditySensor_1=25.5,object_temperatureSensor_0=21,object_temperatureSensor_1=19.3,rxInfo_0_gatewayID="b827ebfffeaa4582",rxInfo_0_loRaSNR=7.5,rxInfo_0_name="local",rxInfo_0_rssi=-60,rxInfo_0_uplinkID="659cbfab-3216-42fd-9f71-f2c470b7f9da",rxInfo_1_gatewayID="ca925641ce08b33e",rxInfo_1_loRaSNR=4.2,rxInfo_1_name="local",rxInfo_1_rssi=-98,rxInfo_1_uplinkID="15ca3b44-17a0-4662-82c9-aa23b40e16eb",txInfo_dr=5,txInfo_frequency=868500000 1677100755000000000

```

Is this what you expect?

---

<div class="post-metadata">

**Author:** ![Patric\_Dahl](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/patric_dahl/32/19604_2.png) [@Patric\_Dahl](https://community.influxdata.com/u/Patric_Dahl)\
**Post date:** [February 25, 2025, 1:05pm UTC](https://community.influxdata.com/t/json-v2-array-parsing/28529/7 "2025-02-25T13:05:39Z")

</div>

Hi!

I would predicate some support how to structure below json with eighter \_v2 or XPath.  
Goal is to have a well structured Prometheus data to later build Grafana dashboard.

```auto
[
    {
        "id": "32af28d7",
        "name": "CHA_1",
        "site": "CHA",
        "site_id": "15",
        "site_external": "cha",
        "executable": "idac",
        "hostname": "hostname",
        "host_address": "10.10.10.1",
        "num_cpu": 4,
        "mem_free": 14537523200,
        "mem_used": 1834655744,
        "mem_total": 16728846336,
        "disk_free": 135360729088,
        "disk_used": 25674330112,
        "disk_total": 161035059200,
        "version": "6.12.4",
        "container_info": {
            "container_id": "f36373737437436436436",
            "container_name": "config-idac-1",
            "image_id": "sha256:4634643643643643634646",
            "image_digest": "sha256:3464364364364364363463468",
            "image_tag": "latest",
            "repository": "domain.com"
        },
        "networks": [
            "127.0.0.1/8",
            "100.100.100.2/24",
            "::1/128"
        ],
        "upgrade_version": "sha256:4364367436346346",
        "upgrade_semver": "6.18.3",
        "upgradable": true,
        "upgrade_progress": "done",
        "updated_at": "2025-02-24T14:14:15.538652889Z",
        "version_hash": "346436346436",
        "upstream_sni": "tcp.io",
        "upstream": "tcp.io:443",
        "activity_status": "online"
    },
    {
        "id": "3b0f74ce",
        "name": "CHA_3",
        "site": "CHA",
        "site_id": "15",
        "site_external": "cha",
        "executable": "idac",
        "hostname": "hostname",
        "host_address": "10.10.10.1",
        "num_cpu": 4,
        "mem_free": 14413262848,
        "mem_used": 1958907904,
        "mem_total": 16728850432,
        "disk_free": 135810621440,
        "disk_used": 25224437760,
        "disk_total": 161035059200,
        "version": "6.12.4",
        "container_info": {
            "container_id": "36436436346436436437a",
            "container_name": "config-idac-1",
            "image_id": "sha256:8c561346346743634664478fc7011e6a9f8f487",
            "image_digest": "sha256:3b84074f84d556e8536345643654366549aeeb5498b6636e4cfdbd8",
            "image_tag": "latest",
            "repository": "domain.com"
        },
        "networks": [
            "127.0.0.1/8",
            "100.100.100.2/24",
            "::1/128"
        ],
        "upgrade_version": "sha256:59601ec34643634612dbc57fb73e23d4235f",
        "upgrade_semver": "6.18.3",
        "upgradable": true,
        "upgrade_progress": "done",
        "updated_at": "2025-02-24T14:14:29.100831472Z",
        "version_hash": "90c9a3146af46e2f4f36436436436build-1736669421",
        "upstream_sni": "tcp.io",
        "upstream": "tcp.io:443",
        "activity_status": "online"
    } 
]

```
