fix(parsers.json_v2): Remove BOM before parsing (#11926)

This commit is contained in:
Joshua Powers 2022-10-12 14:08:56 -05:00 committed by GitHub
parent 08c1ce9cb6
commit 7c1d1755d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 1 deletions

View File

@ -2,16 +2,19 @@ package json_v2
import (
"fmt"
"io"
"strconv"
"strings"
"time"
"github.com/dimchansky/utfbom"
"github.com/tidwall/gjson"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/plugins/parsers/temporary/json_v2"
"github.com/tidwall/gjson"
)
// Parser adheres to the parser interface, contains the parser configuration, and data required to parse JSON
@ -68,6 +71,13 @@ func (p *Parser) Init() error {
}
func (p *Parser) Parse(input []byte) ([]telegraf.Metric, error) {
reader := strings.NewReader(string(input))
body, _ := utfbom.Skip(reader)
input, err := io.ReadAll(body)
if err != nil {
return nil, fmt.Errorf("unable to read body after BOM removal: %v", err)
}
// Only valid JSON is supported
if !gjson.Valid(string(input)) {
return nil, fmt.Errorf("invalid JSON provided, unable to parse")