new way to flatten json
Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com>
This commit is contained in:
@@ -26,28 +26,25 @@ 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] = ""
|
|
||||||
} else {
|
|
||||||
out[key] = fmt.Sprintf("%v", child)
|
out[key] = fmt.Sprintf("%v", child)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func flattenJSONFile(inputFile string, outputFile string) error {
|
func flattenJSONFile(inputFile string, outputFile string) error {
|
||||||
in, err := os.Open(inputFile)
|
in, err := os.Open(inputFile)
|
||||||
|
|||||||
Reference in New Issue
Block a user