Jeffery Grajkowski
Jeffery Grajkowski

Reputation: 4061

Serializing a JSON string as JSON in Scala/Play

I have a String with some arbitrary JSON in it. I want to construct a JsObject with my JSON string as a JSON object value, not a string value. For example, assuming my arbitrary string is a boring {} I want {"key": {}} and not {"key": "{}"}.

Here's how I'm trying to do it.

val myString = "{}"
Json.obj(
  "key" -> Json.parse(myString)
)

The error I get is

type mismatch; found :
  scala.collection.mutable.Buffer[scala.collection.immutable.Map[String,java.io.Serializable]]
  required: play.api.libs.json.Json.JsValueWrapper

I'm not sure what to do about that.

Upvotes: 1

Views: 897

Answers (1)

Dragisa Krsmanovic
Dragisa Krsmanovic

Reputation: 596

"{}" is an empty object.

So, to get {"key": {}} :

Json.obj("key" -> Json.obj())

Update:

Perhaps you have an old version of Play. This works under Play 2.3.x:

scala> import play.api.libs.json._
scala> Json.obj("foo" -> Json.parse("{}"))
res2: play.api.libs.json.JsObject = {"foo":{}}

Upvotes: 2

Related Questions