Skip to content

Instantly share code, notes, and snippets.

@gonyykim
gonyykim / flattenJson.kt
Last active October 26, 2023 12:02
flattenJson.kt
fun flattenJson(json: Map<String, Any>, parentKey: String = ""): Map<String, Any> {
val result = mutableMapOf<String, Any>()
for ((key, value) in json) {
val fullKey = if (parentKey.isEmpty()) key else "$parentKey.$key"
when (value) {
is Map<*, *> -> {
// If the value is another JSON object, recursively flatten it
val nestedMap = value as Map<String, Any>