0%
August 8, 2024

Access Nested Properties from Json String in Kotlin

kotlin

Gson: JSON String into POJO

Dependencies of Gson
implementation("com.google.code.gson:gson:2.11.0")
Example 1

Let's take an example of Event from Stripe sdk, in which I want to access the value of key orderId in

// via event.dataObjectDeserializer.`object`.get().toJson()
{
    ...
    "meatadata": {
        "orderId": "cx_a34_sfFddazadDFfd"
    },
    ...
}
  • The general approach is to reduce whatever object we have into a json string.

  • After a json string is obtained, we model it by a sequence of data class's.

    data class EventObject(val metadata: MetaData)
    data class MetaData(val orderId: String)
  • We parse by Gson:

    import com.google.gson.Gson
    
    val eventObj = Gson().fromJson(
        event.dataObjectDeserializer.`object`.get().toJson(), // <-- the json string
        EventObject::class.java
    )
    val orderId = eventObj.metadata.orderId

JSONObject and kotlinx.serialization: Map JSON String POJO

This is intensively used in my jwt util class in this blog post.

Dependencies of org.json and kotlinx-serialization-json
plugins {
    kotlin("plugin.serialization") version "2.0.0"
}

dependencies {
    implementation("org.json:json:20240303")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.1")
}
Example 2
import org.json.JSONObject
import kotlinx.serialization.json.Json

val stringPayload = JSONObject(jwt.otherClaims).toString() // jwt.otherClaims is a Map<String, Any>
val tokenPayload = Json.decodeFromString<JwtPayload>(stringPayload)
Remark
  • When stringPayload contains unknown keys compared to JwtPayload, we should use

    Json { ignoreUnknownKeys = true }

    instead of simply Json. This is a usual case because sometimes we are just concerned about certain keys inside a json, we don't intent to completely list all of them.

  • The Gson.fromJson() and Json{ ignoreUnknownKeys = true } approaches are interchangeable.