From 4bace2bb105683b13d52793aedf2519c16343b47 Mon Sep 17 00:00:00 2001 From: Hadi <112569860+anotherhadi@users.noreply.github.com> Date: Sun, 28 Sep 2025 00:45:01 +0200 Subject: [PATCH] new json flatten algo Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com> --- leak-utils/misc/json.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/leak-utils/misc/json.go b/leak-utils/misc/json.go index b2c8a62..0c0b03e 100644 --- a/leak-utils/misc/json.go +++ b/leak-utils/misc/json.go @@ -17,14 +17,27 @@ func flattenJSON(prefix string, in map[string]any, out map[string]any) { if prefix != "" { key = prefix + "." + k } + switch child := v.(type) { case map[string]any: flattenJSON(key, child, out) case []any: - for i, item := range child { - tempMap := make(map[string]any) - tempMap[fmt.Sprintf("%d", i)] = item - flattenJSON(key, tempMap, out) + if len(child) == 0 { + out[key] = "" + } else { + for i, item := range child { + tempMap := make(map[string]any) + subKey := fmt.Sprintf("%d", i) + + if obj, ok := item.(map[string]any); ok { + flattenJSON(key+"."+subKey, obj, out) + } else if arr, ok := item.([]any); ok { + tempMap[subKey] = arr + flattenJSON(key, tempMap, out) + } else { + out[key+"."+subKey] = fmt.Sprintf("%v", item) + } + } } default: out[key] = fmt.Sprintf("%v", child)