poc JsonExtractSlice

This commit is contained in:
Sebastien Blot 2022-04-20 09:49:32 +02:00
parent d9f111c4ca
commit 493e238455
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
}*/