Skip to content
Snippets Groups Projects
flattenJson.go 505 B
Newer Older
package helper

func FlattenJSON(prefix string, input map[string]interface{}, output map[string]interface{}) {
	for key, value := range input {
		// Build the new key
		newKey := key
		if prefix != "" {
			newKey = prefix + "_" + key
		}

		// Check the type of value
		switch v := value.(type) {
		case map[string]interface{}:
			// If it's a nested JSON object, recursively flatten it
			FlattenJSON(newKey, v, output)
		default:
			// Otherwise, add it to the output map
			output[newKey] = v
		}
	}
}