val json: JsValue = Json.parse(input)
println(json)
prints:
{"id":1,"command":"connect"}
val command = Json.stringify((json \ "command").get)
println(command)
prints:
"connect"
what am I doing wrong that it prints "connect" (with quotes), rather than connect?
val command = (json \ "command").get.toString
println(command)
also prints:
"connect"
command match {
case "connect" =>
println("connected")
case _ =>
println("unknown command")
prints:
unknown command
what am I doing wrong that it prints "connect" (with quotes), rather than connect?
(json \ "command").get
returns JsValue. When you call Json.stringify
on JsValue object it gives string representation of JsValue object. Json.stringify
does not convert JsValue object to String type. Using Json.stringify
doesn't tantamount to type conversion, it's just a textual representation of the JsValue object.
From the playframework doc , the preferred way to convert from a JsValue to another type is by using its validate method. You will need something like as below:
val command = (json \ "command").validate[String].getOrElse("unknown command")
println(command) // prints connect without quotes