Skip to content

Commit 1cdf588

Browse files
sspainkreimda
authored andcommitted
Update json_v2 parser to handle null types (#9368)
(cherry picked from commit daec104)
1 parent 72340a4 commit 1cdf588

File tree

5 files changed

+75
-18
lines changed

5 files changed

+75
-18
lines changed

plugins/parsers/json_v2/parser.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -306,31 +306,35 @@ func (p *Parser) expandArray(result MetricNode) ([]MetricNode, error) {
306306
return nil, err
307307
}
308308
} else {
309-
if !result.Tag && !result.IsObject() {
310-
if result.SetName == p.currentSettings.TimestampKey {
311-
if p.currentSettings.TimestampFormat == "" {
312-
err := fmt.Errorf("use of 'timestamp_query' requires 'timestamp_format'")
313-
return nil, err
314-
}
315-
timestamp, err := internal.ParseTimestamp(p.currentSettings.TimestampFormat, result.Value(), p.currentSettings.TimestampTimezone)
316-
if err != nil {
317-
return nil, err
309+
if result.SetName == p.currentSettings.TimestampKey {
310+
if p.currentSettings.TimestampFormat == "" {
311+
err := fmt.Errorf("use of 'timestamp_query' requires 'timestamp_format'")
312+
return nil, err
313+
}
314+
timestamp, err := internal.ParseTimestamp(p.currentSettings.TimestampFormat, result.Value(), p.currentSettings.TimestampTimezone)
315+
if err != nil {
316+
return nil, err
317+
}
318+
result.Metric.SetTime(timestamp)
319+
} else {
320+
switch result.Value().(type) {
321+
case nil: // Ignore JSON values that are set as null
322+
default:
323+
if result.Tag {
324+
result.DesiredType = "string"
318325
}
319-
result.Metric.SetTime(timestamp)
320-
} else {
321326
v, err := p.convertType(result.Value(), result.DesiredType, result.SetName)
322327
if err != nil {
323328
return nil, err
324329
}
325-
result.Metric.AddField(result.OutputName, v)
326-
}
327-
} else if !result.IsObject() {
328-
v, err := p.convertType(result.Value(), "string", result.SetName)
329-
if err != nil {
330-
return nil, err
330+
if result.Tag {
331+
result.Metric.AddTag(result.OutputName, v.(string))
332+
} else {
333+
result.Metric.AddField(result.OutputName, v)
334+
}
331335
}
332-
result.Metric.AddTag(result.OutputName, v.(string))
333336
}
337+
334338
results = append(results, result)
335339
}
336340

plugins/parsers/json_v2/parser_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func TestData(t *testing.T) {
6969
name: "Test multiple timestamps",
7070
test: "multiple_timestamps",
7171
},
72+
{
73+
name: "Test field with null",
74+
test: "null",
75+
},
7276
}
7377

7478
for _, tc := range tests {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file,id=ak0217l8ue0x,type=Feature detail="https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/ak0217l8ue0x.geojson",mag=1.5,place="63 km N of Petersville, Alaska",status="automatic",time=1623708726566,updated=1623709998223,url="https://earthquake.usgs.gov/earthquakes/eventpage/ak0217l8ue0x"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"type": "FeatureCollection",
3+
"metadata": {
4+
"generated": 1623710450000,
5+
"url": "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson",
6+
"title": "USGS All Earthquakes, Past Hour",
7+
"status": 200,
8+
"api": "1.10.3",
9+
"count": 10
10+
},
11+
"features": [
12+
{
13+
"type": "Feature",
14+
"properties": {
15+
"mag": 1.5,
16+
"place": "63 km N of Petersville, Alaska",
17+
"time": 1623708726566,
18+
"updated": 1623709998223,
19+
"tz": null,
20+
"url": "https://earthquake.usgs.gov/earthquakes/eventpage/ak0217l8ue0x",
21+
"detail": "https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/ak0217l8ue0x.geojson",
22+
"felt": null,
23+
"cdi": null,
24+
"mmi": null,
25+
"alert": null,
26+
"status": "automatic"
27+
},
28+
"id": "ak0217l8ue0x"
29+
}
30+
],
31+
"bbox": [
32+
-157.5749,
33+
32.9001667,
34+
0.25,
35+
-115.6211667,
36+
66.331,
37+
132.5
38+
]
39+
}
40+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[[inputs.file]]
2+
files = ["./testdata/null/input.json"]
3+
data_format = "json_v2"
4+
[[inputs.file.json_v2]]
5+
[[inputs.file.json_v2.object]]
6+
path = "features"
7+
tags = ["type", "id"]
8+
disable_prepend_keys = true

0 commit comments

Comments
 (0)