Convert serialized Kotlin data class to JSON Object

So I was working on a project a few days ago and I had to display a list of items programmatically, an example would make things clearer.

Imagine we have a data class that contains a List<String> of attributes that describes a footballer, eg shooting, heading, stamina, etc. and we wanted to display all this info in a Chip. Note that these attributes can change from the remote data source hence we cannot predefine textviews that would display the info (eg we cannot do “shooting: [append the value of shooting here]”)

val tvShoot = tv_shoot
tvShoot.text = "Shooting: ${player.shooting}"

we cannot do the above because our JSON response looks somewhat like this

    "attributes": {
        "power": "87",
        "stamina": "82",
        "shooting": "91",
        "heading": "70",
        "dribble": "88",
        "diving": "65",
        "technique": "80"

we could equally have the attributes of the next player come in like this.

    "attributes": {
        "shielding": "87",
        "sliding": "92",
        "shooting": "61",
        "heading": "90",
        "tackle": "80",
        "command": "65",
        "technique": "70"

the first response would look like the attributes we can see on an attacker while the second is more suited to a defender.

So if we wanted to get all these details out at one go lets mock the response we would get from our server.

We would need to add the Gson dependency to help in our Json processing, so add the following to your build.gradle file

implementation ''

The following code simulates a response we would get from the server via Retrofit (that is beyond the scope of this example), you can also create a data class from Json2Kotlin

val attribute = "{\n" +
                "    \"attributes\": {\n" +
                "        \"shielding\": \"87\",\n" +
                "        \"sliding\": \"92\",\n" +
                "        \"shooting\": \"61\",\n" +
                "        \"heading\": \"90\",\n" +
                "        \"tackle\": \"80\",\n" +
                "        \"command\": \"65\",\n" +
                "        \"technique\": \"70\"\n" +
                "    }\n" +

val attr = Gson().fromJson(features,

now we have our attribute class created and the value passed to attr, we can access the value in attr by doing


but recall that the next response that comes in might be for a defender and not an attacker, hence we can get the values dynamically while using the @SerializedName(“shooting”) naming convention.

val chipGroup = xml_defined_chipgroup
Gson().toJson(attr).let { value ->
    JSONObject(value).also { json ->
        for (key in json.keys()) {
            val chip = Chip(context)
            chip.text = "$key : ${json.getString(key)}"
  • Firstly we convert the values of the class to a JSON string
  • Next, we convert JSON string to proper formatted JSON Object
  • Then we use the key as placeholders just like
tvShoot.text = "Shooting: ${player.shooting}"

while we append the values.

So there you go, now you can convert your sting based lists on the fly using GSON.

Total Page Visits: 1073 - Today Page Visits: 1

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *