new way to flatten json

Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com>
This commit is contained in:
Hadi
2025-09-28 00:49:47 +02:00
parent d501ddad33
commit fb90fcb125

View File

@@ -26,25 +26,22 @@ func flattenJSON(prefix string, in map[string]any, out map[string]any) {
out[key] = "" out[key] = ""
} else { } else {
for i, item := range child { for i, item := range child {
tempMap := make(map[string]any) subKey := fmt.Sprintf("%s.%d", key, i)
subKey := fmt.Sprintf("%d", i)
if obj, ok := item.(map[string]any); ok { switch itemValue := item.(type) {
flattenJSON(key+"."+subKey, obj, out) case map[string]any:
} else if arr, ok := item.([]any); ok { flattenJSON(subKey, itemValue, out)
tempMap[subKey] = arr case []any:
flattenJSON(key, tempMap, out) tempMap := make(map[string]any)
} else { tempMap["array"] = itemValue
out[key+"."+subKey] = fmt.Sprintf("%v", item) flattenJSON(subKey, tempMap, out)
default:
out[subKey] = fmt.Sprintf("%v", item)
} }
} }
} }
default: default:
if child == nil { out[key] = fmt.Sprintf("%v", child)
out[key] = ""
} else {
out[key] = fmt.Sprintf("%v", child)
}
} }
} }
} }