Reading a json file(approx 180 MB) without key names from S3 to a data class using object mapper (in kotlin)

140 Views Asked by At

My sample json in s3 looks like this :

{"380682":{"ids":[380682, 765894, 9875086]},"1995539":{"ids":[1995539, 234567, 987654]}}

I want to map it to List of Activities where:

data class Activities(
    val key: String,
    val values: List<String>
)

My application requires me to use Object Mapper only.

Currently I am doing it this way but it takes way too long:

val activitiesList = mutableListOf<Activities>()
    val response: Map<String, Map<String, List<String>>>
    val reader: Reader = InputStreamReader(S3ObjectInputStream, StandardCharsets.UTF_8)
    val jsonNode: JsonNode

    reader.use { jsonNode = objectMapper.readTree(it) }
    response = objectMapper.convertValue(jsonNode, object : TypeReference<Map<String, Map<String, List<String>>>>() {})

    for ((key, value) in response) {
        value.get("ids")?.let {
            Activities(
                key = key,
                values = it)
        }?.let { ActivitiesList.add(it) }

Please suggest a faster and cleaner way to do this...

0

There are 0 best solutions below