I’m building a telegraf config to parse JSON data from OpenWeatherMap API. I’m seeing unexpected behavior, where some fields are duplicated. This is a problem when writing to Prometheus because it will cause duplicate timestamp errors. Could someone please explain why the JSON parser works this way? Here’s an example telegraf config:
[[inputs.file]]
files = ["./openweathermap.json"]
data_format = "json_v2"
[[inputs.file.json_v2]]
[[inputs.file.json_v2.tag]]
path = "coord.lon"
[[inputs.file.json_v2.tag]]
path = "coord.lat"
[[inputs.file.json_v2.object]]
path = "main"
[[inputs.file.json_v2.object]]
path = "wind"
[[inputs.file.json_v2.field]]
path = "visibility"
The output looks like this; the visibility
field appears twice:
> file,lat=34.1355,lon=-116.0543 feels_like=299.75,humidity=28,pressure=1003,temp=300.56,temp_max=301.56,temp_min=297.02,visibility=10000 1718608445000000000
> file,lat=34.1355,lon=-116.0543 deg=307,gust=7.15,speed=3.58,visibility=10000 1718608445000000000
Example JSON:
{
"coord": {
"lon": -116.0543,
"lat": 34.1355
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}
],
"base": "stations",
"main": {
"temp": 300.56,
"feels_like": 299.75,
"temp_min": 297.02,
"temp_max": 301.56,
"pressure": 1003,
"humidity": 28
},
"visibility": 10000,
"wind": {
"speed": 3.58,
"deg": 307,
"gust": 7.15
},
"clouds": {
"all": 0
},
"dt": 1718605565,
"sys": {
"type": 2,
"id": 2020992,
"country": "US",
"sunrise": 1718541141,
"sunset": 1718593069
},
"timezone": -25200,
"id": 5404198,
"name": "Twentynine Palms",
"cod": 200
}