I am fairly new to scala and functional programming. I have this piece of code written by someone which constructs json using json4s library,
val json = "body" -> ...toList.map {
case (title, attrs) =>
("name", tag) ~ ("attributes", attrs)
}
val finalJson = compact(render(json))
All i understood on seeing this is, it constructs a json with given fields.
If attrs
has value then its constructs json body with name and attributes.
But if attrs
is null, then I get below exception.
java.lang.NullPointerException:
at org.json4s.JsonDSL$class.map2jvalue(JsonDSL.scala:71)
at org.json4s.JsonDSL$.map2jvalue(JsonDSL.scala:61)
Searched several so posts and couldnt get hold of how to get it working. Closest one i got is this post
I think I have to utilize None
and Option
. Any ideas on how to fix this and probably an explanation would be really helpful if i misunderstood something.