You want to create several options for flatMap:
maybeString flatMap { json => json.asOpt[String] map { str => // do something with it str } } getOrElse "0"
Or as a for understanding:
(for { json <- maybeString str <- json.asOpt[String] } yield str).getOrElse("0")
I would also advise working with the value inside the map and passing the parameter around, so that None will be processed by your controller and displayed on BadRequest , for example.
source share