1

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.

1 Answer 1

2

"{}" 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":{}}
Sign up to request clarification or add additional context in comments.

3 Comments

Nope, sorry. That was just an example for my arbitrary string.
Sorry. I didn't understand your question then.
Huh. So in describing my problem I simplified out a detail I thought wasn't important but was. For some unknown reason I was actually doing Json.obj("foo" -> Map("bar" -> Json.parse("{}"))) when I should be doing Json.obj("foo" -> Json.obj("bar" -> Json.parse("{}"))). Your answer helped me reevaluate my assumptions so thank you for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.