I have a JSON payload that contains the field name as part of the JSON field’s value.
JSON Payload:
{
"results": [
{
"fields": [
{
"fieldName": "employeeId",
"value": "123"
},
{
"fieldName": "name",
"value": "John"
},
{
"fieldName": "salary",
"value": 100023
},
{
"fieldName": "isDifferentlyAbled",
"value": true
},
{
"fieldName": "weight",
"value": 10.12
}
... // more fields, around 15
]
},
{
... // 2nd node
}
]
}
I can deserialize it by converting the payload by using ObjectMapper and by writing a logic that contains conditional statements such as:
if (fieldNameNode.asText().equals("employeeId")) {
employee.setId(fieldValue.asText());
}
But as the payload contains roughly 15 fields this solution is not elegant. is there a way to deserialize such payload elegantly using the library itself? or by using any design pattern?