1

I am trying to parse json using case class but running into an issue.

I have the following json

{
 "general": {
    "table": "123",
 },

 "employee" : {
    "table": "employee_data"
 },

 "fulltime" : {
    "table": "fulltime_employee_data"
 },

 "consultant" : {
    "table": "consultant_employee_data"
 } 
}

Here's my case class:

case class EmployeeInfo(employees: List[Map[String, String]])

I'm trying to parse the above json using the case class using the following code. It returns the object as null.

val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(new JavaTimeModule())
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
val str = Source.fromFile("employeeInfo.json").mkString
val temp = mapper.readValue[EmployeeInfo](str)

temp here is being returned as null. My json seems to be list of maps which is what I provided in my case class. Any thoughts on what I'm missing?

1 Answer 1

1

I figured it out. My case class needed the variable name as defined in the json.

case class EmployeeInfo(general: TableDetails, employee: TableDetails, fulltime: TableDetails, consultant: TableDetails)

class TableDetails {

val table: String = "" 

//getter and setter for table field

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.