@@ -15,10 +15,10 @@ import (
1515// Config Errors
1616const (
1717 ErrExpressionsOrRegexRequired = "JMES expressions or regex is required"
18- ErrCouldNotCompileJMES = "could not compile JMES expression"
19- ErrEmptyJSONStageConfig = "empty json stage configuration"
20- ErrEmptyJSONStageSource = "empty source"
21- ErrMalformedJSON = "malformed json"
18+ ErrCouldNotCompileJMES = "could not compile JMES expression"
19+ ErrEmptyJSONStageConfig = "empty json stage configuration"
20+ ErrEmptyJSONStageSource = "empty source"
21+ ErrMalformedJSON = "malformed json"
2222)
2323
2424// JSONConfig represents a JSON Stage configuration
@@ -143,58 +143,25 @@ func (j *jsonStage) processEntry(extracted map[string]any, entry *string) error
143143 return errors .New (ErrMalformedJSON )
144144 }
145145
146- for n , e := range j .expressions {
147- r , err := e .Search (data )
146+ for name , expr := range j .expressions {
147+ rawResult , err := expr .Search (data )
148148 if err != nil {
149149 if Debug {
150150 level .Debug (j .logger ).Log ("msg" , "failed to search JMES expression" , "err" , err )
151151 }
152152 continue
153153 }
154-
155- switch r .(type ) {
156- case float64 :
157- // All numbers in JSON are unmarshaled to float64.
158- extracted [n ] = r
159- case string :
160- extracted [n ] = r
161- case bool :
162- extracted [n ] = r
163- case nil :
164- extracted [n ] = nil
165- default :
166- // If the value wasn't a string or a number, marshal it back to json
167- jm , err := json .Marshal (r )
168- if err != nil {
169- if Debug {
170- level .Debug (j .logger ).Log ("msg" , "failed to marshal complex type back to string" , "err" , err )
171- }
172- continue
173- }
174- extracted [n ] = string (jm )
154+ value , err := j .simplifyType (rawResult )
155+ if err == nil {
156+ extracted [name ] = value
175157 }
176158 }
177159 if j .regex .String () != "" {
178- for key , value := range data {
160+ for key , rawValue := range data {
179161 if j .regex .MatchString (key ) {
180- switch value .( type ) {
181- case float64 :
162+ value , err := j . simplifyType ( rawValue )
163+ if err == nil {
182164 extracted [key ] = value
183- case string :
184- extracted [key ] = value
185- case bool :
186- extracted [key ] = value
187- case nil :
188- extracted [key ] = nil
189- default :
190- jm , err := json .Marshal (value )
191- if err != nil {
192- if Debug {
193- level .Debug (j .logger ).Log ("msg" , "failed to marshal complex type back to string" , "err" , err )
194- }
195- continue
196- }
197- extracted [key ] = string (jm )
198165 }
199166 }
200167 }
@@ -205,6 +172,31 @@ func (j *jsonStage) processEntry(extracted map[string]any, entry *string) error
205172 return nil
206173}
207174
175+ // extractWithType returns the value if it's a simple type (string, number, bool),
176+ // otherwise, it returns it as a JSON string
177+ func (j * jsonStage ) simplifyType (value any ) (any , error ) {
178+ switch value .(type ) {
179+ case float64 :
180+ return value , nil
181+ case string :
182+ return value , nil
183+ case bool :
184+ return value , nil
185+ case nil :
186+ return nil , nil
187+ default :
188+ // If the value wasn't a string or a number, marshal it back to json
189+ jm , err := json .Marshal (value )
190+ if err != nil {
191+ if Debug {
192+ level .Debug (j .logger ).Log ("msg" , "failed to marshal complex type back to string" , "err" , err )
193+ return nil , err
194+ }
195+ }
196+ return string (jm ), nil
197+ }
198+ }
199+
208200// Cleanup implements Stage.
209201func (* jsonStage ) Cleanup () {
210202 // no-op
0 commit comments