crowdsec/pkg/exprhelpers/jsonextract.go
Thibault bui Koechlin aad2e1421e fix printf
2020-05-25 11:35:32 +02:00

37 lines
789 B
Go

package exprhelpers
import (
"strings"
"github.com/buger/jsonparser"
log "github.com/sirupsen/logrus"
)
func JsonExtractLib(jsblob string, target ...string) string {
value, dataType, _, err := jsonparser.Get(
jsonparser.StringToBytes(jsblob),
target...,
)
if err != nil {
log.Errorf("jsonExtractLib : %+v : %s", target, err)
return ""
}
if dataType == jsonparser.NotExist {
log.Debugf("%+v doesn't exist", target)
return ""
}
strvalue := string(value)
//debug stuff
return strvalue
}
func JsonExtract(jsblob string, target string) string {
if !strings.HasPrefix(target, "[") {
target = strings.Replace(target, "[", ".[", -1)
}
fullpath := strings.Split(target, ".")
log.Debugf("extract path %+v", fullpath)
return JsonExtractLib(jsblob, fullpath...)
}