I am getting the following flat structure from mongo db document as in source object and I need to group a few fields based on a pattern to prepare an arraylist and set it to the target object.
{
"_id": 1,
"clientId": "1001",
"grant1Id": "1",
"grant1Type" : "A",
"grant1Amount": "100",
"grant2Id": "2",
"grant2Type": "B",
"grant2Amount": "200",
"grant3Id": "3",
"grant3Type": "C",
"grant3Amount": "300",
.....so on
}
The grant# can go upto 20 objects. So, I want to combine all grant fields and prepare an array.
The output should look like this:
{
"_id": 1,
"clientId": "1001",
"grants": [
{
"grantId": "1",
"grantType" : "A",
"grantAmount": "100",
},
{
"grantId": "2",
"grantType" : "B",
"grantAmount": "200",
},
{
"grantId": "3",
"grantType" : "C",
"grantAmount": "300",
}
]
}
Source object structure:
public class Source {
private String id;
private String clientId;
private String grant1Id;
private String grant1Type;
private String grant1Amount;
...so on
}
Target object:
public class Target {
private String id;
private String clientId;
private List<Grant> grants;
}
Any help will be appreciated.
Thanks.