new json flatten algo
Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com>
This commit is contained in:
@@ -17,14 +17,27 @@ func flattenJSON(prefix string, in map[string]any, out map[string]any) {
|
|||||||
if prefix != "" {
|
if prefix != "" {
|
||||||
key = prefix + "." + k
|
key = prefix + "." + k
|
||||||
}
|
}
|
||||||
|
|
||||||
switch child := v.(type) {
|
switch child := v.(type) {
|
||||||
case map[string]any:
|
case map[string]any:
|
||||||
flattenJSON(key, child, out)
|
flattenJSON(key, child, out)
|
||||||
case []any:
|
case []any:
|
||||||
for i, item := range child {
|
if len(child) == 0 {
|
||||||
tempMap := make(map[string]any)
|
out[key] = ""
|
||||||
tempMap[fmt.Sprintf("%d", i)] = item
|
} else {
|
||||||
flattenJSON(key, tempMap, out)
|
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:
|
default:
|
||||||
out[key] = fmt.Sprintf("%v", child)
|
out[key] = fmt.Sprintf("%v", child)
|
||||||
|
|||||||
Reference in New Issue
Block a user