0
votes

my json is a JsValu

// this is a list of JsObject extracted from a json
val parents = (json \ "parents").as[List[JsObject]]

// a map between parent name to list of their kids
val kidsNamesMap = (json \ "kids").as[Map[String,List[String]]]

// creating a new JsObject
 val newParent = parent + ("kids" -> Json.toJson(kidsNamesMap.getOrElse(parentName,"")))

when compiling i get an error:

No Json serializer found for type Object. Try to implement an implicit Writes or Format for this type. val newParent = parent + ("kids" -> Json.toJson(kidsNamesMap.getOrElse(parentName,"")))

but im now sure what writer to add, wrote writer before for my case class but here im not sure...

thanks

1
Thats because kidsNamesMap.getOrElse(parentName,"") will not really have a type. From the map you either get a List[String] or you get a string (.getOrElse). The ideal way would be to replace this as: val newParent = parent + ("kids" -> { kidsNamesMap.get(parentName).map(Json.toJson(_)).getOrElse(JsString("")) }) - Ashesh
please add it as answer :) - JohnBigs
@JohnBiggs done. - Ashesh

1 Answers

1
votes

Thats because kidsNamesMap.getOrElse(parentName,"") will not really have a type. From the map you either get a List[String] or you get a String (.getOrElse). The ideal way would be to replace this as:

val newParent = parent + ( "kids" -> { kidsNamesMap.get(parentName).map(Json.toJson(_)).getOrElse(J‌​sString("")) } )