score:50
JsObject
has a +
method that allows you to add fields to an object, but unfortunately your jsonObject
is statically typed as a JsValue
, not a JsObject
. You can get around this in a couple of ways. The first is to use as
:
scala> jsonObject.as[JsObject] + ("c" -> Json.toJson(3))
res0: play.api.libs.json.JsObject = {"a":1,"b":2,"c":3}
With as
you're essentially downcasting—you're telling the compiler, "you only know that this is a JsValue
, but believe me, it's also a JsObject
". This is safe in this case, but it's not a good idea. A more principled approach is to use the OWrites
directly:
scala> val jsonObject = classAWrites.writes(classAObject)
jsonObject: play.api.libs.json.JsObject = {"a":1,"b":2}
scala> jsonObject + ("c" -> Json.toJson(3))
res1: play.api.libs.json.JsObject = {"a":1,"b":2,"c":3}
Maybe someday the Json
object will have a toJsonObject
method that will require a OWrites
instance and this overly explicit approach won't be necessary.
score:-1
simpler way is to use argoanut (http://argonaut.io/)
var jField : Json.JsonField = "myfield" //Json.JsonField is of type String
obj1.asJson.->:(jField, obj2.asJson) // adds a field to obj1.asJson
here obj1.asJson
creates a JSON object
and obj2
is the object to be added to the json created by obj1.asJson
score:8
I found a solution myself. In fact the JsValue, which is the return type of Json.toJson has no such method, but the JsObject (http://www.playframework.com/documentation/2.2.x/api/scala/index.html#play.api.libs.json.JsObject) does, so the solution is:
val jsonObject = Json.toJson(classAObject).as[JsObject]
jsonObject + ("c", JsNumber(3))
I hope someone will find this useful :)
Source: stackoverflow.com
Related Query
- Play Framework - add a field to JSON object
- Play Scala JSON - conditionally add field to JSON object in Writes
- How to implement implicit Json Writes of embedded object in Play Framework 2.x
- Scala + Play Framework + Slick - Json as Model Field
- How to read and write Anorm object with the new JSON API in Play Framework 2.1-RC2?
- Add a Json object to JSON Array using scala play
- Add field to existing JSON object with sprayJSON (scala)
- Play Json add field which is not present in class
- Play framework scala form object for password field binding?
- Play Framework Json Object mapping partial objects
- How to implement implicit Json Writes of Future object in Play Framework 2.x
- Using java classes in Play framework with Scala to be returned as Json Object
- Play Json framework can't parse json string if some field doesn't exist
- How do I use a filter to add a domain object to the request scope in Play Framework 2.x
- How can we create case class object from json in scala + play framework 2.0
- How to convert Squeryl query object to JSON - Play Framework
- How to convert Squeryl query object to JSON - Play Framework
- parsing a Json Array in play framework JsObject
- Joda DateTime Field on Play Framework 2.0's Anorm
- Scala to JSON in Play Framework 2.1
- Play Framework & JSON Web Token
- How to render JSON response in Play framework v2.0 (latest build from GIT)
- Dependency injection with abstract class and object in Play Framework 2.5
- How to respond with a pretty-printed JSON object using play framework?
- Play Framework Scala format large JSON (No unapply or unapplySeq function found)
- Custom JSON validation constraints in Play Framework 2.3 (Scala)
- Play 2 - Can't return Json object in Response
- Play Framework JSON Format for Case Objects
- Play framework JSON reads: How to read either String or Int?
- How to add a prefix to all my routes in Play Framework 2?
More Query from same tag
- DateTimeFormatter.format with Continent/City
- Scala, casbah aggregate query
- NoClassDefFoundError running tests in SBT with scoverage plugin
- Receiving error when I add the scala nature to project in Eclipse
- Simple Iteration over case class fields
- Objects Extending Scala traits. Ruby equivalent?
- How to initialize function type parameter in Scala class?
- wordcount.scala error
- Scala: To check the current Timstamp is greater than a timestamp column in my dataframe
- Joining two tables in spark that are present in hive
- Is there a way to omit processing over a RDD partition with few elements in Spark?
- Iteration over a sealed trait in Scala?
- How to get absolute URL of action in controller via Scala Routing DSL?
- How to convert JsValue to a Model Class with Scala in Play framework?
- object time is not a member of package org.joda
- How to specify custom database-connection parameters for testing purposes in Play Framework v2?
- How do I extract values from a kafka row via spark under structured streaming?
- Meaning of Play Application class
- scala: default values for function with type parameters; used in partial applied context
- Using Scala with Java in Android Studio
- Implement implicit Writes for result of join query
- How to convert rogue query into Mysql ?
- How to create pairs of nodes in Spark?
- Define Compound Task in SBT
- Scala - check files from a List[File] what is the last modified
- Convert Resultset to Dataframe
- How to serialize an akka "actorRef" that is part of a case class using JsonFormat custom serializer from spray-json?
- Transform Future[Object] to Future[S]
- What is the idiomatic scala way to add more details to the thrown exceptions wrapped in Try instances
- Using TypeTags to Check if Expected Type Matches Actual