Help with json_v2 object of objects parsing

Hello all,
I am stuck and I hope somebody could help me. I have got the following json and I am trying to parse it with json_v2:

{
           "records" : {
                   "123": {
                           "Max Size" : 209715200,
                           "Size" : 115065412354048,
                   },
                   "456": {
                           "Max Size" : 5554307072,
                           "Size" : 24002560,
                   }
           }
   }

Is there any way I could receive the following metrics:

measurment1,ZID=123 Max_Size=209715200,Size=115065412354048 TIMESTAMP
measurment1,ZID=456 Max_Size=5554307072,Size=24002560 TIMESTAMP

?
In the documentation is stated “Object: Every key-value in a object is treated as a single metric”, so it seems I have to convert “records” to list, the only viable way I can think of is to use records.@values - but I lose the keys that way and cant figure a way to convert them to tags and link them with the measurements.

yeah I think we more commonly see situations like this with a list of objects like:

{
    "records" : [
            {
                    "Name": "123",
                    "Max Size" : 209715200,
                    "Size" : 115065412354048
            },
            {
                    "Name": "456",
                    "Max Size" : 5554307072,
                    "Size" : 24002560
            }
        ]
}

which is then very easy to parse:

  [[inputs.file.json_v2]]
    [[inputs.file.json_v2.object]]
      path = "records"
      tags = ["Name"]
file,Name=123 Max_Size=209715200,Size=115065412354048 1679322335000000000
file,Name=456 Max_Size=5554307072,Size=24002560 1679322335000000000

@Ludmil_Stamboliyski I guess you are aware that the data you posted is not valid JSON (trailing comma) but I guess it’s due to copy’n pasting it from a larger sample… :wink:

Here is the config you are looking for

[[inputs.file]]
  files = ["test_configs/jsontest_forum_ludmil_stamboliyski.json"]

  data_format = "xpath_json"

  xpath_native_types = true

  [[inputs.file.xpath]]
    metric_name = "'measurment1'"
    metric_selection = "//records/*"
    field_selection = "*"
    field_name = "replace(name(.), ' ', '_')"

    [inputs.file.xpath.tags]
        ZID = "name(.)"

You probably have to replace the inputs.file part with your parent plugin…

1 Like