Deserialize String inside JSON

Viewed 159

I am getting a JSON response with the following structure:

[
    {
        "dateCreated": "07-22-2021",
        "comments": "Commenst",
        "url_list": "[{\"id\":1,\"name\":\"\",\"img\":\"https://legacynissan-ez360.imgix.net/1C3CDFEB6GD555251/1C3CDFEB6GD555251-2016-Dodge-Dart.sp_turntable_pic.1000.16x9-3840x2160-4K.20210721130782.jpg?w=300\",\"is_checked\":false,\"pois\":[]},{\"id\":2,\"name\":\"\",\"img\":\"https://legacynissan-ez360.imgix.net/1C3CDFEB6GD555251/1C3CDFEB6GD555251-2016-Dodge-Dart.sp_turntable_pic.1001.16x9-3840x2160-4K.20210721130782.jpg?w=300\",\"is_checked\":false,\"pois\":[]},{\"id\":3,\"name\":\"\",\"img\":\"https://legacynissan-ez360.imgix.net/1C3CDFEB6GD555251/1C3CDFEB6GD555251-2016-Dodge-Dart.sp_turntable_pic.1002.16x9-3840x2160-4K.20210721130782.jpg?w=300\",\"is_checked\":false,\"pois\":[]}]"

    } 
]

So basically I have a list of objects (this one contains dateCreated, comments, and url_list) my problem is that url_list is a list of objects saved as a string and I do not know how to deserialize that string into an object.

1)Is this possible? 2)How can it be done?

I am using Ktor client and kotlinx Serialization.

3 Answers

I am using Ktor client and kotlinx Serialization.

I've never used either. So I googled "kotlinx custom serialization". That led me to the documentation on the the GitHub page for Kotlinx serialization. On that page there is a huge section on custom serialization.

Based on that, your answers are:

  1. Is this possible? -> Yes, quite.

  2. How can it be done? -> Write a custom serializer - per the documentation - that parses the url_list JSON string into a class object.

You will need to mock the url_list as its own object. You may have to modify it a little.

@Serializable
data class YourJson(
        var dateCreated: String,
        var comments: String
        @SerializedName("url_list")
        var urlStuff : List<UrlStuff>
)
@Serializable
data class UrlStuff(
        var id: Int,
        var name: String,
        @SerializedName("img")
        var image: String,
        @SerializedName("is_checked")
        var isChecked: Boolean,
        var pois: List<String>
)

I ended up creating 3 objects to model the data that was coming in.

The data on the response was structured as follows:

  1. A list of objects
  2. Each one of those contains a list of objects embedded in a string
  3. Each one of those contains another list of objects.

My three objects match the response I am getting now usually I can get away with just marking everything with @serializable and having the classes done right and Kotlin will take care of the rest.

The problem this time is that 2nd object is embedded in a string.

The way to fix this was to parse it using this:

Json.decodeFromString<List<myObject>>(theStringIWantToParse)

that fixes the issue and I can get all the data, however, that is very inconvenient because I wouldn't have it all in one data structure and I would have to manually parse that every time.

so I combined it with information from this this video

That lead me to create another 3 objects that are built the way I want them (I called append them with Entity) and not the way the response gives them to me then 3 mapper classes to map the 3 response objects (I append these with Dto) to my own 3 objects (the entities)

The downsides:

  1. More code (3 entity and 3 mapper classes)

The benefits are:

  1. I have a clear layer where I can adapt fast and simply to chances on the response structure.
  2. It provides a bit more structure to my codebase so it is easier to manage than random parsing here and there
Related