I am receiving JSON from a 3rd party, which I cannot control. Assume I have the following Java classes:
class Student {
String id;
Name name;
List<Course> courses;
}
class Name {
String first;
String middle;
String last;
}
class Course {
String subject;
String number;
}
I would expect the JSON for a list of Students to look something like this:
{
"students":[
{
"id":"00123",
"name":{
"first":"John",
"middle":"Quincy",
"last":"Adams"
},
"courses":[
{
"subject":"MATH",
"number":"101"
}
]
}
]
}
However, the JSON I am receiving is in a completely "flat" format (no nested objects). All of the data is "associated" through patterns in the names of the keys. For example:
{
"student_1_ID_groupA": "00123",
"name_1_StudentID_groupA": "00123"
"name_1_FirstName_groupA": "John",
"name_1_MiddleName_groupA": "Quincy",
"name_1_LastName_groupA": "Adams",
"course_1_StudentID_groupA": "00123",
"course_1_Subject_groupA": "MATH",
"course_1_Number_groupA": "101",
"student_2_ID_groupA": "00456",
"name_2_StudentID_groupA": "00456"
"name_2_FirstName_groupA": "Edgar",
"name_2_MiddleName_groupA": "Allen",
"name_2_LastName_groupA": "Poe",
"course_2_StudentID_groupA": "00456",
"course_2_Subject_groupA": "LIT",
"course_2_Number_groupA": "201",
"student_1_ID_groupB": "00789",
"name_1_StudentID_groupB": "00789"
"name_1_FirstName_groupB": "Martin",
"name_1_MiddleName_groupB": "Luther",
"name_1_LastName_groupB": "King",
"course_1_StudentID_groupB": "00789",
"course_1_Subject_groupB": "BIO",
"course_1_Number_groupB": "120",
"course_2_StudentID_groupB": "00789",
"course_2_Subject_groupB": "CHEM",
"course_2_Number_groupB": "120",
}
The key names are made up of four parts, which could easily be parsed with regex:
- The object type
- A numeric identifier
- The field name
- A group
The objects are uniquely identified by combining their numeric identifier and their group. Student 1 from groupA is a different object than Student 1 from groupB. Similarly, Student 1 from groupA is a different object than Student 2 from groupA.
Ultimately, I need to deserialze this JSON into a list of students.
List<Student> students; // contains all Students defined above
I've tried to find examples of solving a similar problem, but with no luck. I must admit, I am at a loss for where to start. I know Jackson supports a lot of customization, like custom deserializers. However, I would greatly appreciate if somebody could let me know if such a task is even possible using Jackson, and if it is, provide some pointers to get started.