Search code examples
jsonscalaspray-json

How to ignore a key from json while mapping in case class


If i have a JSON

{"name":"jack","email":"jackt@gmail.com"}

now while mapping this JSON i want only name to be mapped in my case class, for example

case class Person(name:String)

How can we achieve this?


Solution

  • import spray.json._
    
    
    case class Person(name: String)
    object PersonFormat extends DefaultJsonProtocol {
      implicit val personFormat = jsonFormat1(Person)
    }
    import PersonFormat._
    
    val source = """{ "name":"jack", "email":"jackt@gmail.com" }"""
    source.parseJson.convertTo[Person]
    

    You need to define the JsonProtocol with a implicit val.