How to get nested JsonObject from Decoded JWT in Kotlin?

Viewed 33

I have already decode the token but i can't get the nested JsonObject. I want to get id in user_metadata:hasura.

{
  "exp": 16636813,
  "email": "123@gmail.com",
  "user_metadata": {
   "hasura": {
      "id": "123"
  }
}

I have used couple of Library, I have gotten Option<String> or Claim. But i couldn't go further than gettin only string value from "exp".

I have tried this from this library (https://github.com/auth0/JWTDecode.Android), but i got null values:

var claim : Claim = jwt.getClaim("user_metadata")
metadata = claim.asObject(UserMetadata::class.java)!!
1 Answers

My problem was that i was trying to claim my object as a UserMetadata class but actually it was JsonObject.

I have solved my question with this way:

var json : JsonObject = jwt.getClaim("user_metadata").asObject(JsonObject::class.java)!!
var gsonn = Gson()
var testModel = gsonn.fromJson(jsonn.toString(),UserMetadata::class.java)
var id : String = testModel.hasura.id
Related