Json metric not captured

Hi,

I have create a python script that captures some data and manipulates them in order to show them in the correct way. The output of the script is the following

{"workers": [{"miner": "mrig1", "hashrate": 2.1}, {"miner": "mrig2", "hashrate": 4.44}, {"miner": "mrig3", "hashrate": 3.28}], "hashes": 59039282877, "hashrate": 35.74}

In my telegraf.conf file I have added

[[inputs.exec]]
    commands = ["python3 /opt/mining.py"]
    timeout = "20s"
    interval = "120s"
    data_format = "json"
    name_suffix = "_miner"
    tag_keys = "miner"
    json_string_fields = "miner"

My problem is that I cannot capture the miner tag from the json response. Any help will be appreciated.

thank you

I would use the json_query to process only the list of workers:

[[inputs.exec]]
  commands = ["python3 /opt/mining.py"]
  timeout = "20s"
  interval = "120s"
  name_suffix = "_miner"

  json_query = "workers"
  tag_keys = ["miner"]
  data_format = "json"

thanks Daniel, that did the trick.

What if I want to capture also the “hashes” key but with no specific tags?

I don’t think there is a good way to do this, using the json_query argument selects the subsection of the json, and we lose access to the higher up values. We have an issue open to make a more advanced parser though (#1363).