Amani Amani - 3 months ago 25
JSON Question

How to map a JSON string to Kotlin Map

I have a simple Kotlin program that access a Mongo database and produce a JSON string as below;

"{
"_id" : { "$oid" : "593440eb7fa580d99d1abe85"} ,
"name" : "Firstname Secondname" ,
"reg_number" : "ATC/DCM/1016/230" ,
"oral" : 11 ,
"oral_percent" : 73 ,
"cat_1" : 57 ,
"cat_2" : 60 ,
"cat_average" : 59 ,
"assignment" : 90
}"


How do I map this in Kotlin Map/MutableMap? Is there an API in Kotlin to read JSON and map it to Map/MutableMap?

Answer Source

This can be done with Klaxon. With this you can easily read the Json data as JsonObject which is actually a MutableMap.

val json: JsonObject = Parser().parse(jsonData) as JsonObject