Using play-json you can convert Json to Map using validate. Validate returns JsResult which can be JsSuccess(data,path) or JsError(errors). Pattern match to get the map out of it.
Check if str is of case 1 if not fallback to case 2
Json.parse(jsonString).validate[Map[String, String]].getOrElse(json.validate[List[Map[String,String]]])
Case 1: For Map[String, String]
import play.api.libs.json._
val result = Json.parse(someValidJsonString).validate[Map[String, String]]
result match {
case JsSuccess(map, _) => map
case JsError(errors) => Logger.error("json parsing failed")
}
Case 2: For List[Map[String, String]]
import play.api.libs.json._
val result = Json.parse(someValidJsonString).validate[List[Map[String, String]]]
result match {
case JsSuccess(list, _) => list
case JsError(errors) => Logger.error("json parsing failed")
}
Here someValidJsonString is the json string you want to convert to Map
If you are using sbt project then add play-json dependency to your project.
build.sbt
libraryDependencies ++= Seq("com.typesafe.play" %% "play-json" % "2.5.4")
Scala REPL
scala> import play.api.libs.json._
import play.api.libs.json._
scala> val map = Map("java" -> 1, "scala" -> 2)
map: scala.collection.immutable.Map[String,Int] = Map(java -> 1, scala -> 2)
scala> Json.toJson(map).validate[Map[String, Int]]
res3: play.api.libs.json.JsResult[Map[String,Int]] = JsSuccess(Map(java -> 1, scala -> 2),)
scala> val result = Json.toJson(map).validate[Map[String, Int]]
result: play.api.libs.json.JsResult[Map[String,Int]] = JsSuccess(Map(java -> 1, scala -> 2),)
scala> result match { case JsSuccess(data, _) => data case JsError(errors) => Unit}
res4: Object = Map(java -> 1, scala -> 2)
map("key1")("key2")("key3")returnsvalue3??? how is that? where didkey3came from??