crowdsec/pkg/exprhelpers/jsonextract.go
Thibault "bui" Koechlin dbb420f79e
local api (#482)
Co-authored-by: AlteredCoder
Co-authored-by: erenJag
2020-11-30 10:37:17 +01: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.Tracef("extract path %+v", fullpath)
return JsonExtractLib(jsblob, fullpath...)
}