JohnBigs
JohnBigs

Reputation: 2811

update JsObject value for a specific key

lets say I have this simple json as JsObject:

val simpleJson = Json.parse("""{
                       "name" : "Watership Down"
                       }""".stripMargin).as[JsObject]

and I want to change the "name" value, how would I do that on JsObject?

Upvotes: 0

Views: 73

Answers (2)

Zvi Mints
Zvi Mints

Reputation: 1142

As @Levi Ramsey said, just without the Play Json wrappers:

simpleJson ++ Json.obj("name" -> "Spaceship Up");

From Play 2.4.X you can use +:

simpleJson + ("name" -> "Spaceship Up");

https://scastie.scala-lang.org/bvAXZw8TSTetuWJcMeQU0Q

Upvotes: 1

Levi Ramsey
Levi Ramsey

Reputation: 20561

simpleJson ++ JsObject(Map("name": JsString("Spaceship Up")))

++ merges two JsObjects, with the right-hand-side taking precedence.

Upvotes: 0

Related Questions