Compare commits

...

1 commit

Author SHA1 Message Date
Sebastien Blot 493e238455 poc JsonExtractSlice 2022-04-20 09:49:32 +02:00
2 changed files with 28 additions and 0 deletions

View file

@ -44,6 +44,7 @@ func GetExprEnv(ctx map[string]interface{}) map[string]interface{} {
"JsonExtract": JsonExtract,
"JsonExtractUnescape": JsonExtractUnescape,
"JsonExtractLib": JsonExtractLib,
"JsonExtractSlice": JsonExtractSlice,
"File": File,
"RegexpInFile": RegexpInFile,
"Upper": Upper,

View file

@ -1,6 +1,7 @@
package exprhelpers
import (
"encoding/json"
"strings"
"github.com/buger/jsonparser"
@ -58,3 +59,29 @@ func JsonExtract(jsblob string, target string) string {
log.Tracef("extract path %+v", fullpath)
return JsonExtractLib(jsblob, fullpath...)
}
func JsonExtractSlice(jsblob string, target string) []interface{} {
if !strings.HasPrefix(target, "[") {
target = strings.Replace(target, "[", ".[", -1)
}
fullpath := strings.Split(target, ".")
log.Tracef("extract path %+v", fullpath)
bContent := JsonExtractLib(jsblob, fullpath...)
ret := make([]interface{}, 0)
json.Unmarshal([]byte(bContent), &ret)
return ret
}
/*func JsonExtractMap(jsblob string, target string) map[string]interface{} {
if !strings.HasPrefix(target, "[") {
target = strings.Replace(target, "[", ".[", -1)
}
fullpath := strings.Split(target, ".")
log.Tracef("extract path %+v", fullpath)
bContent := JsonExtractLib(jsblob, fullpath...)
ret := make(map[string]interface{}, 0)
json.Unmarshal([]byte(bContent), &ret)
return ret
}*/