Files
mq/utils/json.go
2025-07-31 09:31:28 +05:45

46 lines
874 B
Go

package utils
import (
"github.com/oarkflow/json"
"github.com/oarkflow/json/jsonparser"
)
func RemoveFromJSONBye(jsonStr json.RawMessage, key ...string) json.RawMessage {
return jsonparser.Delete(jsonStr, key...)
}
func RemoveRecursiveFromJSON(jsonStr json.RawMessage, key ...string) json.RawMessage {
var data any
if err := json.Unmarshal(jsonStr, &data); err != nil {
return jsonStr
}
for _, k := range key {
data = removeKeyRecursive(data, k)
}
result, err := json.Marshal(data)
if err != nil {
return jsonStr
}
return result
}
func removeKeyRecursive(data any, key string) any {
switch v := data.(type) {
case map[string]any:
delete(v, key)
for k, val := range v {
v[k] = removeKeyRecursive(val, key)
}
return v
case []any:
for i, item := range v {
v[i] = removeKeyRecursive(item, key)
}
return v
default:
return v
}
}