MCPcopy
hub / github.com/crowdsecurity/crowdsec / jsonExtractType

Function jsonExtractType

pkg/exprhelpers/jsonextract.go:83–110  ·  view source on GitHub ↗
(jsblob string, target string, t jsonparser.ValueType)

Source from the content-addressed store, hash-verified

81}
82
83func jsonExtractType(jsblob string, target string, t jsonparser.ValueType) ([]byte, error) {
84 if !strings.HasPrefix(target, "[") {
85 target = strings.ReplaceAll(target, "[", ".[")
86 }
87 fullpath := strings.Split(target, ".")
88
89 log.Tracef("extract path %+v", fullpath)
90
91 value, dataType, _, err := jsonparser.Get(
92 jsonparser.StringToBytes(jsblob),
93 fullpath...,
94 )
95 if err != nil {
96 if errors.Is(err, jsonparser.KeyPathNotFoundError) {
97 log.Debugf("Key %+v doesn't exist", target)
98 return nil, fmt.Errorf("key %s does not exist", target)
99 }
100 log.Errorf("jsonExtractType : %s : %s", target, err)
101 return nil, fmt.Errorf("jsonExtractType: %s : %w", target, err)
102 }
103
104 if dataType != t {
105 log.Errorf("jsonExtractType : expected type %s for target %s but found %s", t, target, dataType.String())
106 return nil, fmt.Errorf("jsonExtractType: expected type %s for target %s but found %s", t, target, dataType.String())
107 }
108
109 return value, nil
110}
111
112// func JsonExtractSlice(jsblob string, target string) []interface{} {
113func JsonExtractSlice(params ...any) (any, error) {

Callers 2

JsonExtractSliceFunction · 0.85
JsonExtractObjectFunction · 0.85

Calls 3

TracefMethod · 0.80
GetMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…