1

Need to map multiple types of JSON responses to a single POJO so that I can compare the different objects to provide insight about the differences.

I had tried mapping the first response to the POJO and parsed the second response to populate the defined POJO:

    class XXX {
        @JsonProperty("accountHolder")
        private String accountHolder;
        @JsonProperty("routingNumber")
        private String routingNumber;
        @JsonProperty("balance")
        private List<Balance> balance;
        @JsonProperty("accountName")
        private String accountName;
        @JsonProperty("bankTransferCodeType")
        private String bankTransferCodeType;
        @JsonProperty("individualInformation")
        private IndividualInformation individualInformation;
        @JsonProperty("acctType")
        private String acctType;
        @JsonProperty("transactionList")
        private TransactionList transactionList;
        @JsonProperty("accountNumber")
        private String accountNumber;
        @JsonProperty("uniqueId")
        private String uniqueId;
        @JsonProperty("bankNetID")
        private String bankNetID;
        @JsonIgnore
        private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    }

First response:


[
    {
        "ACCOUNT_NAME": "",
        "ACCOUNT_NUMBER": "",
        "AVAILABLE_BALANCE": null,
        "CURRENT_BALANCE": "",
        "FULL_ACCOUNT_NUMBER": null,
    }
]

Second response:

"bankAccount": [
      {
        "accountName": "",
        "accountNumber": "",
        "routingNumber": "",
        "fullAccountNumber": "",
        "bankTransferCodeType": "",
        "acctType": "",
        "transactionList": {
          "transaction": [
            {
              "amount": {
                "curCode": "",
                "content": ""
              }
          ],
          "oldestTxnDate": ""
        },
        "uniqueId":
      }
}

Expecting a generic way to map the different structured JSON entities to single POJO.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
Subrat Pani
  • 43
  • 1
  • 7

5 Answers5

2

It doesn’t seems to have any generic way. But you can do this:

  • Create multiple domain classes for each response type
  • Create a single standard domain class
  • Create mapper for each response class to map that to standard domain class. You can use MapStruct reference here
Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
2

How to map multiple JSON responses to a single Java POJO?

As both responses seem to be completely different from each other, with nothing in common, I would refrain from attempting to use a single class for reading both responses.

Expecting a generic way to map the different structured JSONs to single POJO.

  1. You could parse both responses as a Map<String, Object> and then map the values to a common class.

  2. You could create separated classes for mapping each response. It will allow you to decouple them and evolve them as you need. You also can use use mapping frameworks such as MapStruct for reducing the boilerplate code when mapping from one object to another.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
1

I would suggest using Jackson Json Views. Here is an example for the same :

Example

public class Views {

    public class Global {
    }

    public class Internal extends Global {
    }
}

class XXX {

    @JsonView(Views.Global.class)
    @JsonProperty("accountHolder")
    private String accountHolder;

    @JsonView(Views.Internal.class)
    @JsonProperty("routingNumber")
    private String routingNumber;

}

Hope it helps.

George Z.
  • 6,643
  • 4
  • 27
  • 47
Antariksh
  • 508
  • 8
  • 17
0

What I did is I created a MyResponse model containing basically all response fields from the JSON response you expect to get. MyResponse has c-tor or receiving these fields or setters allowing setting them.

Then I created some kind of service class MyService that can issue multiple requests and gets responses back.

Then you just do something like this in some kind of manager class or whatever you call it:

MyService mySer = new MyService();

MyResponse myRes = new MyResponse(
  mySer.getDetails(),
  mySer.getPicture(),
  mySer.getSomethingElse()
);

These calls (getDetails, getPicture...) send requests to end point and return responses which are then just mapped into the the fields of MyResponse class constructor. This happens by the framework so MyResponse has annotations @XmlRootElement and @XmlAccessorType of type FIELD to ensure that happens. If for whatever reason, you dont want to create response containing result of getPicture for example, you just assign null to that imput parameter.

pixel
  • 9,653
  • 16
  • 82
  • 149
0

I suggest to use @JsonProperty("") and @JsonAlias("").

 class XXX {
    @JsonAlias("accountName")
    @JsonProperty("ACCOUNT_NAME")
    private String name;

    @JsonAlias("routingNumber")
    @JsonProperty("ROUTING_NUMBER")
    private String routing;}

I hope it helps.

New Bee
  • 390
  • 3
  • 10