How to replace a JSON value in Play

后端 未结 5 1902
遥遥无期
遥遥无期 2020-12-31 01:58

How do I replace a value in a JSON value in Play?
Code to illustrate:

def newReport() = Action(parse.json) { request =>
    var json = request.bod         


        
5条回答
  •  自闭症患者
    2020-12-31 02:52

    Something along the lines of:

    val updatedJson = if((request.body \ "customerId").as[Int] == -1){
      val newId = JsObject(Seq(("customerId",JsString("ID12345"))))
      (request.body ++ newId).as[JsValue]
    } else request.body
    
    updatedJson.validate[Report](Reports.readsWithoutUser).map {
      case _: Report =>
    

提交回复
热议问题