I have a following scenario:
case class Person(id: Int, name: String)
val json = Json.obj("id" -> 1, "name" -> "John", "address"-> "Paris", "contact" -> "1234")
Here, I want to extract extra (key,value) from json i.e {"address"-> "Paris", "contact" -> "1234"} that do not belong to Person.
I have developed following approach so far:
case class Person(id: Int, name: String)
  val personReads = Json.reads[Person]
  val personWrites = Json.writes[Person]
  val json = Json.obj("id" -> 1, "name" -> "John", "address"-> "Paris", "contact" -> "1234")
  val person: Person = personReads.reads(json).get
  // This person json does not have extra fields 
  val personJson: JsObject = personWrites.writes(person).asInstanceOf[JsObject]
  val extraKeys = json.keys.diff(personJson.keys)
  val extraJson = extraKeys.foldLeft(Json.obj()){(result,key) =>
                            result.+(key -> json.\(key).get)}
  // {"address":"Paris","contact":"1234"}
This works but here I have to do a lot of json to case class conversions. What would be the best way to extract extra (key,value) in this scenario ?
                        
If you want to make it general for any case class and not doing anything fancy with custom
Reads, you can use reflection or shapeless to extract case class names and then remove those from an object you're trying to parse.E.g. using reflection, this only creates a case class instance once and does not require
Writesat all:And use it e.g. like so:
Runnable code is available there