How can I convert a json string to a scala map? How can I convert a json string to a scala map? json json

How can I convert a json string to a scala map?


I tried the following method with json4s 3.2.11 and it works:

import org.json4s._import org.json4s.jackson.JsonMethods._//...def jsonStrToMap(jsonStr: String): Map[String, Any] = {  implicit val formats = org.json4s.DefaultFormats  parse(jsonStr).extract[Map[String, Any]]}

Maybe you didn't define the implicit val of type Formats? Note also that you don't need to have an implicit val within every and each method as long as it's findable in the scope.


You can use the following code to parse a JSON string into a Map[String, Any]

val jsonMap = parse(jsonString).values.asInstanceOf[Map[String, Any]]

However, this is not typesafe and hence should be used with caution when extracting values from the map.