Telegraf: how to add additional (custom) tag when parsing JSON file

Hey,

I am struggling with this for the last couple of days.

Let’s say, I have JSON:


{
  "SN": "3323FF9D",
  "Datas": [
    [
      224.2,
      8.79
    ],
    [
      227,
      14.14
    ],
    [
      227.1,
      8.21
    ]
  ]
}

Each (first) numerical value in the array is basically voltage of three phases. I would like to add entry to influx database that would have SN as tag (which is easy), also voltage value of each phase. But I wish to add voltage from JSON as _value field, and also add additional tag key field with a name of entity_id and my own value like a_phase b_phase etc.

Something like this (sorry, I made this line manually, so it might not be correct):

device=3323FF9D,entity_id=a_phase_voltage [this one is custom, it is not in JSON] (and all of this with a value from JSON 224.2)

I should result in “entry” with two tags device, entity_id and _value of 224.2

I tried something like this, but not sure, how to approach a_phase_voltage problem…

[[inputs.mqtt_consumer.json_v2]]
    [[inputs.mqtt_consumer.json_v2.field]]
      path = "Datas.0.0"
      rename = "a_phase_voltage"
      type = "float"

This method creates just value field and I do not know, how to add that entity_id tag with a value that basically is custom, not from JSON.

Hope you understand…

Thanks

Hi,

Here is how I would start:

[[inputs.file]]
  files = ["metrics.json"]
  data_format = "xpath_json"

  xpath_native_types = true

  [[inputs.file.xpath]]
    metric_name = "'metric'"
    field_selection = "descendant::*[not(*)]"
    field_name_expansion = true
  [inputs.file.xpath.tags]
    device = "SN"

This would produce a metric like:

metric,device=3323FF9D Datas_0_0=224.2,Datas_0_1=8.79,Datas_1_0=227,Datas_1_1=14.14,Datas_2_0=227.1,Datas_2_1=8.21,SN="3323FF9D" 1717101133000000000

You could then rename the various Datas_0_0 to whatever you want. In fact, I would probably not have the phase as a tag, but update the field name to each phase.

However, if you really do want different metrics for each phase, then I would use starlark to split this up into three, one for each phase as you want.

Does that make sense?

@srebhan any other ideas?