I want to create a Java POJO class to store a list of different objects from JSON using the Jackson library.
As you can see below JSON code, this list contains 2 objects (Person type and Construct type). Each type or each object has different field names. The problem is that each object is proceeded with the same field "party".
How can Jackson differentiate each object (Party or Construct) from just reading the field name "party"?
My POJO class for Party is:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Party {
public String party;
public String partyId;
public PartyType partyType;
public PartyLifecycleState partyLifecycleState;
public PartyLifecycleEvent partyLifecycleEvent;
}
My POJO class for Person is:
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonTypeName("PersonStruct")
public class PersonStruct extends PartyStruct {
{ partyType = PartyType.Person; }
public PersonName name;
public USTaxInfo usTaxInfo;
public BioProfile bioProfile;
public CustomerProfile customerProfile;
public ContactInfo contactInfo;
}
My POJO class for Construct is:
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonTypeName("ConstructStruct")
public class ConstructStruct extends PartyStruct{
{ partyType = PartyType.Construct; }
public ConstructName name;
public USTaxInfo usTaxInfo;
public EntityProfile entityProfile;
public ControlsProfile controlsProfile;
public ContactInfo contactInfo;
}
Below is the JSON structure when I call GET request API from the server:
[
"party": {
"partyId": "46cbe5c5-7a14-44be-b87d-cb9e432806ca",
"partyType": "Person",
"partyLifecycleState": "ACTIVE",
"partyLifecycleEvent": "Create",
"name": {
"prefix": "NIL",
"first": "Deandra",
"middle": "Central Interactions Representative",
"last": "Beer",
"suffix": "MRS",
"preferredMoniker": "yasmine.schmeler",
"preferredLegal": "late_keller"
},
"usTaxInfo": {
"tin": "292293219",
"tinType": "SSN",
"backupWithholding": false,
"backupWithholdingRate": 13
},
"bioProfile": {
"birthDate": "2022-07-19",
"primaryCitizenshipCountry": "USA",
"additionalCitizenshipCountries": [
"CANADA",
"FRANCE"
],
"primaryResidenceCountry": "USA",
"additionalResidenceCountries": null
},
"customerProfile": {
"bankingInsider": false,
"politicallyExposedPerson": false,
"riskRating": "Low",
"privacyOptOut": false
},
"contactInfo": {
"addresses": [
{
"id": "0ad4cce2-b4cb-46c7-b765-b088fda50455",
"status": "Inactive",
"primary": false,
"desc": "Work",
"use": "Both",
"kind": "MPO",
"street1": "My Address tf",
"street2": "",
"street3": "",
"city": "New York City",
"sprdco": "square_payne",
"country": "USA",
"postalCode": "1111"
},
{
"id": "24519f30-ab63-4f67-a0d2-ee529dc626b7",
"status": "Inactive",
"primary": false,
"desc": "Alternate",
"use": "Unknown",
"kind": "POBox",
"street1": "My Address Bcp",
"street2": "",
"street3": "",
"city": "New York City",
"sprdco": "pretty_pare",
"country": "USA",
"postalCode": "1111"
}
],
"emails": [
{
"id": "d3b2433c-3a9c-43d0-ab31-e9a07cce19a9",
"status": "Inactive",
"primary": true,
"desc": "ok marginal plane pale apricot chameleon",
"address": "frail_mirzakhani+1@gmail.com"
},
{
"id": "ca795fec-e3a7-4008-bf5e-3bb91183f146",
"status": "Inactive",
"primary": true,
"desc": "available administrative coil motionless fuchsia lobster",
"address": "romantic_banach+1@gmail.com"
}
],
"phones": [
{
"id": "fd101795-56ec-4440-b57a-45d2aef9ce35",
"status": "Active",
"primary": true,
"desc": "pleasant labour salt cruel lemon yak",
"IDDCountryCallingCode": "672",
"number": "110-195-6656",
"extension": "6188"
}
]
}
}
},
{
"party": {
"partyId": "d7c64bf4-5964-4914-ae15-1b2093efa56b",
"partyType": "Construct",
"partyLifecycleState": "ACTIVE",
"partyLifecycleEvent": "Create",
"name": {
"legalName": "sheer_kare",
"tradeName": "inadequate_turing",
"moniker": "random_pasteur"
},
"usTaxInfo": {
"tin": "456468523",
"tinType": "ATIN",
"backupWithholding": false,
"backupWithholdingRate": 65
},
"entityProfile": {
"formationDate": "2022-07-18",
"formationCountry": "af",
"primaryOperatingCountry": "is",
"additionalOperatingCountries": null,
"constructType": "Corporation",
"entityNotes": "helpful various pull lovely plum puffin",
"ubos": [
"fe4bf27f-ecd4-4866-923d-0ce004142b8f"
]
},
"controlsProfile": {
"riskRating": "High",
"authorizedAgents": [
"81003384-553e-4e12-ae27-c2dd667e6e06",
"1ef0cf23-4cf7-42d0-bfe3-eaeeaff8758a"
]
},
"contactInfo": {
"addresses": [
{
"id": "7d9b38c9-c4a5-4ab0-aac5-fac5da23595c",
"status": "Active",
"primary": false,
"desc": "Alternate",
"use": "Physical",
"kind": "DPO",
"street1": "My Address qvAb",
"street2": "",
"street3": "",
"city": "New York City",
"sprdco": "fiscal_boyd",
"country": "USA",
"postalCode": "1111"
}
],
"emails": [
{
"id": "6f6179f4-67cf-4caf-9af2-cf0ce9b93ff6",
"status": "Inactive",
"primary": true,
"desc": "stupid rural transport awful dark snipe",
"address": "light_thompson+1@gmail.com"
}
],
"phones": [
{
"id": "7f68cbed-72ab-477d-a469-6fe184d969e1",
"status": "Active",
"primary": true,
"desc": "doubtful private bridge anonymous apricot anaconda",
"IDDCountryCallingCode": "760",
"number": "1-241-447-7059",
"extension": "6538"
}
]
}
}
}]