Question about parsing nested JSON object

Hello, I am struggling with parsing the JSON string with different levels.

I use <json_query> option and take the following example for my test:

Config:

[[inputs.file]]
  files = ["example"]
  data_format = "json"
  tag_keys = ["first"]
  json_string_fields = ["last"]
  json_query = "obj.friends"

Input:

{
    "obj": {
        "name": {"first": "Tom", "last": "Anderson"},
        "age":37,
        "children": ["Sara","Alex","Jack"],
        "fav.movie": "Deer Hunter",
        "friends": [
            {"first": "Dale", "last": "Murphy", "age": 44},
            {"first": "Roger", "last": "Craig", "age": 68},
            {"first": "Jane", "last": "Murphy", "age": 47}
        ]
    }
}

Output:

file,first=Dale last="Murphy",age=44
file,first=Roger last="Craig",age=68
file,first=Jane last="Murphy",age=47

I am able to retrieve the sub properties like “first”, “last” and “age” under “friends”.
But I do not know how to access the items in “name”: {“first”: “Tom”, “last”: “Anderson”}, which is at the same level as “friends”.

Can anybody help me to find a solution to this issue?
Thanks in advance.

Hello @minyone,
Welcome! Thanks for your question. Have you tried accessing the variables with the correct path syntax? Thanks!

Thank you Anaisdg!

Here is my concrete payload:

{
	"customer":"C123",
	"country":"AA",
	"measurements":[
		{"name":"Sensor_1","value":1.5,"unit":"bar"},
		{"name":"Sensor_2","value":3.0,"unit":"°C"}
	]
}

If I use json_query, I can get the measurement values from all sensors, but not the meta data beyond the measurement scope.

  json_query = "measurements"
  tag_keys = ["name"]
  json_string_fields = ["unit"]

But if I use tag_keys = [“customer”,“country”], the json_query option does not work together.

It seems that I can either get the first-level tags, or the nested one, but not both.

What shall i do to overcome this?

Hello @minyone,
As per the path syntax above, I believe if you remove the json_query then you can access the measurements with

"measurements.#.name"    >> ["Sensor_1","Sensor_2"]

Does that work?

Hello @Anaisdg ,

I hope I have understood you correctly and did the following changes:

tag_keys = [“customer”,“country”]
json_string_fields = [“unit”]

I got this output in influx without sensor name and unit string:

time country customer measurements_0_value measurements_1_value topic
1584375434962628300 Canada C123 1.5 3.0 test_plant/data

Did I make any mistake here?

I think it is not possible with current version.

There is a feature request: Support JSON Path-like json parsing · Issue #1363 · influxdata/telegraf · GitHub

1 Like