JSON v2 parsing - Query array error

The following works in GJSON Playground but generates an invalid TOML syntax in telegraf.conf.

JSON input
{
“devices”: [{
“DETAIL”: “detail”,
“DEVICE_TYPE”: “Printer”,
“DATATIME”: “2022,06,21,19,10,00”,
“dl_err_count”: “4”,
“dl_cpu_load”: “0.47”,
“CURTIME”: “2022,06,21,19,14,18”
}, {
“ISDETAIL”: true,
“DEVICE_TYPE”: “Router”,
“hw_version”: “1.5.0”,
“slave”: 220,
“CURTIME”: “2022,06,21,19,14,19”
}],
“result”: “succeed”
}

In the playground, devices.#(DEVICE_TYPE==“Router”)# returns the second element of the array, but in telegraf.conf

  [[inputs.http.json_v2.object]]
      path = "devices.#(DEVICE_TYPE=="Router")#"

generates a TOML syntax error.

I want to match on a particular ‘device_type’ to then be able to extract items that are specific to that portion of the JSON.

@sspaink does the json v2 parser support the filtering syntax above?

Hello @phill, so I think the issue is that you need to escape the quotes around Router like so:
path = "devices.#(DEVICE_TYPE==\"Router\")#" then the json_v2 parser should work as expected.

I use this visual studio code extension to help catch syntax issues in TOML: Even Better TOML - Visual Studio Marketplace it might help you if you are using vscode.

1 Like

@sspaink Duh! So obvious, I completely missed it. I’m new to GJSON and got too buried in this fancy pattern matching feature to remember good ole 'Coding 101 '.

Yep, I use VS Code and just installed the TOML extension before changing the code. Though it didn’t flag this particular error (before I escaped the inner quotes).

On a related topic: The parsing examples show 4 spaces of indent rather than 2 for typical TOML. E.G.,

 [[inputs.http.json_v2]]
     measurement_name = "abc"
     [[inputs.http.json_v2.object]] 
          path = "whatever"
Is that a convention or a requirement?  Personal preference would have me use 2 spaces throughout.

Thanks!

4 spaces is just convention, there isn’t anything to stop you from using 2 spaces if that is what you prefer it shouldn’t break the TOML config.

1 Like