Extract Options from potentially null JSON values using for expression

后端 未结 4 1799
日久生厌
日久生厌 2021-01-19 02:09

I have a JSON document where some values can be null. Using for expressions in json4s, how I can yield None, instead of nothing?

The following will fail to yield whe

4条回答
  •  半阙折子戏
    2021-01-19 02:36

    According to the documentation,

    Any value can be optional. Field and value is completely removed when it doesn't have a value.

    scala> val json = ("name" -> "joe") ~ ("age" -> (None: Option[Int]))

    scala> compact(render(json))

    res4: String = {"name":"joe"}

    Explaining why your for comprehension doesn't yield anything.
    Of course, a null value is mapped to None internally.

提交回复
热议问题