How to map a JSON string to Kotlin Map - json

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?

No additional library is needed:
val jsonObj = JSONObject(jsonString)
val map = jsonObj.toMap()
where toMap is:
fun JSONObject.toMap(): Map<String, *> = keys().asSequence().associateWith {
when (val value = this[it])
{
is JSONArray ->
{
val map = (0 until value.length()).associate { Pair(it.toString(), value[it]) }
JSONObject(map).toMap().values.toList()
}
is JSONObject -> value.toMap()
JSONObject.NULL -> null
else -> value
}
}

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

Using Jackson's kotlin module, you can create a Map/MutableMap as below:
val jsonString = "{\n" +
" \"_id\": {\n" +
" \"\$oid\": \"593440eb7fa580d99d1abe85\"\n" +
" },\n" +
" \"name\": \"Firstname Secondname\",\n" +
" \"reg_number\": \"ATC/DCM/1016/230\",\n" +
" \"oral\": 11,\n" +
" \"oral_percent\": 73,\n" +
" \"cat_1\": 57,\n" +
" \"cat_2\": 60,\n" +
" \"cat_average\": 59,\n" +
" \"assignment\": 90\n" +
"}"
val map = ObjectMapper().readValue<MutableMap<Any, Any>>(jsonString)
Note: In case you're getting the below compilation error
None of the following functions can be called with the arguments supplied
Please ensure that you have added the the dependency of jackson-module-kotlin (for gradle: compile "com.fasterxml.jackson.module:jackson-module-kotlin:${jackson_version}") and have added the import for the readValue implementation as import com.fasterxml.jackson.module.kotlin.readValue in the place where you're using readValue

This can be done without any third party library:
#Throws(JSONException::class)
fun JSONObject.toMap(): Map<String, Any> {
val map = mutableMapOf<String, Any>()
val keysItr: Iterator<String> = this.keys()
while (keysItr.hasNext()) {
val key = keysItr.next()
var value: Any = this.get(key)
when (value) {
is JSONArray -> value = value.toList()
is JSONObject -> value = value.toMap()
}
map[key] = value
}
return map
}
#Throws(JSONException::class)
fun JSONArray.toList(): List<Any> {
val list = mutableListOf<Any>()
for (i in 0 until this.length()) {
var value: Any = this[i]
when (value) {
is JSONArray -> value = value.toList()
is JSONObject -> value = value.toMap()
}
list.add(value)
}
return list
}
Usage to convert JSONObject to Map:
val jsonObject = JSONObject(jsonObjStr)
val map = jsonObject.toMap()
Usage to convert JSONArray to List:
val jsonArray = JSONArray(jsonArrStr)
val list = jsonArray.toList()
More info is here

This is now also possible with kotlinx.serialization:
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
val input = """{
"_id" : { "some_id" : "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
}"""
val json = Json.parseToJsonElement(input)
val map = json.jsonObject.toMap()

The latest version of the org.json library has JSONObject.toMap() so you can just do
val map = JSONObject(string).toMap()
So just add the org.json:json:20220924 dependency to your project.
If you're writing for Android (and thus have an old version of the org.json library) then the following can be used.
import org.json.JSONArray
import org.json.JSONObject
fun JSONObject.toMap(): Map<String, Any?> =
keys().asSequence().associateWith { key -> toValue(get(key)) }
fun JSONArray.toList(): List<Any?> =
(0 until length()).map { index -> toValue(get(index)) }
private fun toValue(element: Any) = when (element) {
JSONObject.NULL -> null
is JSONObject -> element.toMap()
is JSONArray -> element.toList()
else -> element
}

Related

How do I de-serialize this json?

I am working on a project where I need to access currency rates once a day so I am trying to use this json.
To read this, I am simply getting the text of the URL and then trying to use the JSONReader to de-serialize.
val url = URL("https://www.floatrates.com/daily/usd.json")
val stream = url.openStream()
url.readText()
val jsonReader = JsonReader(InputStreamReader(stream))
jsonReader.isLenient = true
jsonReader.beginObject()
while (jsonReader.hasNext()) {
val codeName:String = jsonReader.nextName()
jsonReader.beginObject();
var code:String? = null
var rate = 0.0
while (jsonReader.hasNext()) {
val name:String = jsonReader.nextName()
when(name){
"code" -> {
code = jsonReader.nextString()
break
}
"rate" -> {
rate = jsonReader.nextDouble()
break
}
else -> {
jsonReader.skipValue()
}
}
code?.let {
rates?.set(it, rate)
}
}
}
jsonReader.endObject();
When I run the code , I get:
Expected BEGIN_OBJECT but was STRING
at
jsonReader.beginObject();
When I try using Gson, with the code below:
var url = URL("https://www.floatrates.com/daily/usd.json").readText()
//url = "[${url.substring(1, url.length - 1)}]"
val gson = Gson()
val currencies:Array<SpesaCurrency> = gson.fromJson(url, Array<SpesaCurrency>::class.java)
I get this error :
Expected BEGIN_OBJECT but was STRING at line 1 column 3 path $[0]
at:
gson.fromJson(url, Array<SpesaCurrency>::class.java)
SpesaCurrency.kt looks like this:
class SpesaCurrency(
val code:String,
val alphaCode:String,
val numericCode:String,
val name:String,
val rate:Float,
val date:String,
val inverseRate:Float
)
Any help is greatly appreciated.
I think there is one
jsonReader.endObject();
missing in your code. That imbalance causes the program to fail after the first object has been read. Add it after the inner
while (jsonReader.hasNext()) {
...
}
loop.

Kotlin gson.fromJSON doesn't allow initialization at declaration

Using Kotlin:
I have a class with different variable names than my JSON field names. When I call gson.fromJSON(string, Array<MyClass>::class.java, the initialization at declaration doesn't occur.
My understand from another post that declaration isn't called with constructor that only the only the constructor is called.
How, then do I easily get my JSON data into my class without calling the gson.fromJSON and the calling my class?
I'm not a very advanced developer. I'm looking for code as simple as possible that is efficient systematically.
What I want to do is:
val gson = Gson()
val seriesFirebaseList = gson.fromJson(seriesJSONArrayString, Array<Series>::class.java).toList()
class Series(Series_Unique_ID: String,
Series_Record_Name: String,
Series_Name: String,
Series_From_Year: Int,
Series_To_Year: Int?,
Is_Active: Int,
Logo_Image_Name: String,
Sort_Order: Int) {
val uniqueId: String = Series_Unique_ID
val recordName: String = Series_Record_Name
val seriesName: String = Series_Name
val fromYear: Int = Series_From_Year
var toYear: Int? = Series_To_Year
var isActive: Boolean = Is_Active == 1
val logoImageName: String = Logo_Image_Name
val sortOrder: Int = Sort_Order
// generated data
var imageFirebaseURLString: String? = null
var isFavoriteSeries = false
}
But, all the variables end up null or default value, not the JSON values.
The only thing I can get to work which seems really inefficient is to call the JSON firebase class then convert that to my class
val gson = Gson()
// create seriesFirebase
val seriesFirebaseList = gson.fromJson(seriesJSONArrayString, Array<SeriesFirebase>::class.java).toList()
val seriesList = mutableListOf<Series>()
seriesFirebaseList.forEach { seriesFirebase ->
val series = Series(seriesFirebase)
seriesList.add(series)
}
// series models
class Series(seriesFirebase: SeriesFirebase) {
val uniqueId: String = seriesFirebase.Series_Unique_ID
val recordName: String = seriesFirebase.Series_Record_Name
val seriesName: String = seriesFirebase.Series_Name
val fromYear: Int = seriesFirebase.Series_From_Year
var toYear: Int? = seriesFirebase.Series_To_Year
var isActive: Boolean = seriesFirebase.Is_Active == 1
val logoImageName: String = seriesFirebase.Logo_Image_Name
val sortOrder: Int = seriesFirebase.Sort_Order
// generated data
var imageFirebaseURLString: String? = null
var isFavoriteSeries = false
}
// used to convert Gson/JSON
class SeriesFirebase(val Series_Unique_ID: String,
val Series_Record_Name: String,
val Series_Name: String,
val Series_From_Year: Int,
val Series_To_Year: Int?,
val Is_Active: Int,
val Logo_Image_Name: String,
val Sort_Order: Int) {
constructor() : this(
"",
"",
"",
0,
2150,
0,
"",
0
)
override fun toString(): String {
return ("series: uniqueId: " + Series_Unique_ID
+ "; recordName: " + Series_Name
+ "; name: " + Series_Name
+ "; fromYear: " + Series_From_Year
+ "; toYear: " + Series_To_Year
+ "; isActive: " + Is_Active
+ "; logoImageName: " + Logo_Image_Name
+ "; sortOrder: " + Sort_Order)
}
}
You can mark fields of Series class with #SerializedName. E.g.
#SerializedName("Series_Record_Name")
val recordName: String

Accessing inner JSON field in Scala

I have JSON string as
{
"whatsNew" : {
"oldNotificationClass" : "WhatsNewNotification",
"notificationEvent" : { ..... },
"result" : {
"notificationCount" : 10
.....
}
},
......
"someEmpty": { },
......
}
I am trying to get notificationCount field using json4s in Scala as follow but notificationCount is coming as empty for all. Any help?
UPDATE
Also if some pair is empty how can I handle empty condition and continue to loop?
Function returning JSON string from file
def getData(): Map[String, AnyRef] = {
val jsonString = scala.io.Source.fromInputStream(this.getClass.getResourceAsStream("/sample.json")).getLines.mkString
val jsonObject = parse( s""" $jsonString """)
jsonObject.values.asInstanceOf[Map[String, AnyRef]]
}
Code to get fields
val myMap: Map[String, AnyRef] = MyDataLoader.getData
for((key, value) <- myMap) {
val id = key
val eventJsonStr: String = write(value.asInstanceOf[Map[String, String]] get "notificationEvent")
val resultJsonStr: String = write(value.asInstanceOf[Map[String, String]] get "result")
//val notificationCount: String = write(value.asInstanceOf[Map[String, Map[String, String]]] get "notificationCount")
}
You can use path and extract like so:
val count: Int = (parse(jsonString) \ "whatsNew" \ "result" \ "notificationCount").extract[Int]
you will need this import for the .extract[Int] to work:
implicit val formats = DefaultFormats
To do it in a loop:
parse(jsonString).children.map { child =>
val count: Int = (child \ "result" \ "notificationCount").extract[Int]
...
}

Fastest way to convert nested String to JSON [duplicate]

Is there a straight forward way to format a JSON string in scala?
I have a JSON String like this:
val json = {"foo": {"bar": {"baz": T}}}
Can I use a function f such that:
f(json) = {
"foo":
{"bar":
{"baz": T}
}
}
I know the formatting I have done in my answer is no perfect, but you get the point. And yes, can it be done without using Play Framework?
In case you are using Play Framework you could use Json.prettyPrint method to format JsValue:
import play.api.libs.json.Json
val str = """{"foo": {"bar": {"baz": "T"}}}"""
val jsValue = Json parse str
// JsValue = {"foo":{"bar":{"baz":"T"}}}
Json prettyPrint jsValue
// String =
// {
// "foo" : {
// "bar" : {
// "baz" : "T"
// }
// }
// }
In case you are using scala.util.parsing.json you have to create such method by yourself. For instance:
def format(t: Any, i: Int = 0): String = t match {
case o: JSONObject =>
o.obj.map{ case (k, v) =>
" "*(i+1) + JSONFormat.defaultFormatter(k) + ": " + format(v, i+1)
}.mkString("{\n", ",\n", "\n" + " "*i + "}")
case a: JSONArray =>
a.list.map{
e => " "*(i+1) + format(e, i+1)
}.mkString("[\n", ",\n", "\n" + " "*i + "]")
case _ => JSONFormat defaultFormatter t
}
val jsn = JSON.parseRaw("""{"foo": {"bar": {"baz": "T"}, "arr": [1, 2, "x"]}, "foo2": "a"}""").get
// JSONType = {"foo" : {"bar" : {"baz" : "T"}, "arr" : [1.0, 2.0, "x"]}, "foo2" : "a"}
format(jsn)
// String =
// {
// "foo": {
// "bar": {
// "baz": "T"
// },
// "arr": [
// 1.0,
// 2.0,
// "x"
// ]
// },
// "foo2": "a"
// }
Note that this is not an efficient implementation.
I thought I read somewhere that Typesafe was considering separating their JSON processing out of Play, so look into that to apply #senia's solution first.
Otherwise, look at Jackson--or more precisely, the Scala wrapper for Jackson:
val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
val writer = mapper.writerWithDefaultPrettyPrinter
val json = writer.writeValueAsString(Object value)
I've also heard that the kids are really into Scala Pickling, which apparently has pretty printing as well.

Format JSON string in scala

Is there a straight forward way to format a JSON string in scala?
I have a JSON String like this:
val json = {"foo": {"bar": {"baz": T}}}
Can I use a function f such that:
f(json) = {
"foo":
{"bar":
{"baz": T}
}
}
I know the formatting I have done in my answer is no perfect, but you get the point. And yes, can it be done without using Play Framework?
In case you are using Play Framework you could use Json.prettyPrint method to format JsValue:
import play.api.libs.json.Json
val str = """{"foo": {"bar": {"baz": "T"}}}"""
val jsValue = Json parse str
// JsValue = {"foo":{"bar":{"baz":"T"}}}
Json prettyPrint jsValue
// String =
// {
// "foo" : {
// "bar" : {
// "baz" : "T"
// }
// }
// }
In case you are using scala.util.parsing.json you have to create such method by yourself. For instance:
def format(t: Any, i: Int = 0): String = t match {
case o: JSONObject =>
o.obj.map{ case (k, v) =>
" "*(i+1) + JSONFormat.defaultFormatter(k) + ": " + format(v, i+1)
}.mkString("{\n", ",\n", "\n" + " "*i + "}")
case a: JSONArray =>
a.list.map{
e => " "*(i+1) + format(e, i+1)
}.mkString("[\n", ",\n", "\n" + " "*i + "]")
case _ => JSONFormat defaultFormatter t
}
val jsn = JSON.parseRaw("""{"foo": {"bar": {"baz": "T"}, "arr": [1, 2, "x"]}, "foo2": "a"}""").get
// JSONType = {"foo" : {"bar" : {"baz" : "T"}, "arr" : [1.0, 2.0, "x"]}, "foo2" : "a"}
format(jsn)
// String =
// {
// "foo": {
// "bar": {
// "baz": "T"
// },
// "arr": [
// 1.0,
// 2.0,
// "x"
// ]
// },
// "foo2": "a"
// }
Note that this is not an efficient implementation.
I thought I read somewhere that Typesafe was considering separating their JSON processing out of Play, so look into that to apply #senia's solution first.
Otherwise, look at Jackson--or more precisely, the Scala wrapper for Jackson:
val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
val writer = mapper.writerWithDefaultPrettyPrinter
val json = writer.writeValueAsString(Object value)
I've also heard that the kids are really into Scala Pickling, which apparently has pretty printing as well.