score:0

Json.obj works fine with None if it is a field of an object as you say:

case class A(aaa: Int, bbb: Option[Int])
defined class A

val a = A(1, None)
a: A = A(1,None)

Json.obj("aaa" -> a.aaa, "bbb" -> a.bbb)
res1: play.api.libs.json.JsObject = {"aaa":1,"bbb":null}

You can make your example work as well if your None is an Option, not just None.type as compiler says:

Json.obj("aaa" -> 111, "bbb" -> None.asInstanceOf[Option[Int]])
res3: play.api.libs.json.JsObject = {"aaa":111,"bbb":null}

Json.obj("aaa" -> 111, "bbb" -> Option[String](null))
res4: play.api.libs.json.JsObject = {"aaa":111,"bbb":null}

Related Query

More Query from same tag