Remove a key from a JsValue in Scala

前端 未结 3 1064
星月不相逢
星月不相逢 2021-02-13 17:34

It\'s probably a very easy question but I\'m having trouble finding a clean/working solution. I just want to remove a field from a json object I have. Let\'s say I have :

3条回答
  •  孤街浪徒
    2021-02-13 18:22

    You can use as Method as[JsObject] with minus - symbol. Like Below.

    body.as[JsObject] - "id"
    

    Below has detailed explanation step by step. Let's see with custom object.

    val json: JsValue = JsObject(Seq(
          "error" -> JsBoolean(false),
          "result" -> JsNumber(calcResult),
          "message" -> JsString(null)
        ))
    

    It can be picked by as Method and removed "-" symbol.

    /* Removing Message Property and keep the value in successResult Variable */
    val successResult = json.as[JsObject] - "message"
    

    Take a look at Body Parser in Scala to know about Choosing an explicit body parser, Combining body parsers and Writing a custom body parser.

提交回复
热议问题