SHinny
SHinny

Reputation: 99

JSON to Java Object Mapping , needed for Spring REST

I am getting the following JSON object via a REST API call and I want to be able to pick the information inside the results JSON array and convert the same in POJO for e.g ExchangeListInfo,RedeemCall ... IncomeInformation only

How do i get this done with Jackson 1-9.0 api, I have written some code snippet as well but I cannot seem to figure out how to get the mapping to work in this complex scenario , Any help is appreciated

{
  "code": 200,
  "results": [
    {
     "_id": "5168521",

    "ExchangeListInfo": {
      "InstrumentListingLevel": "ANC",
      "SettlementCurrency": "UAH"
    },   
    "RedeemCall": {
      "IsMWholeCall": false
    },
    "MsdInformation": {
      "MSDCallPutFlag": "P",
      "MSDMinimumDenomination": 1000.0,
      "MSDCouponFrequency": "Q",
      "MSDBondForm": "B",
      "MSDSovDebtFlag": false,
      "MSDDatedDt": "2007-04-26T00:00:00.000-04:00",
      "MSDMaturityDt": "2010-04-22T00:00:00.000-04:00"
    },
    "SecInformation": {
      "PutFreq": "EQ",
      "ItDivFreq": "QWE",
      "ItBasDysTyp": "4123",
      "PuTyp": "1O"
    },
    "ConversionBasic": {
      "ConversionMandatoryFlag": false
    },
    "IncomeInformation": {
      "AccrualMethod": "ACT/365",
      "NextPayDate": "2010-04-22T00:00:00.000-04:00",
      "CouponDividendRate": 0.0,
      "CouponDividendType": "KAR",
      "CouponDividendCurrency": "9AH",
      "CouponDividendFrequency": 4,
      "LastPayDate": "2010-01-21T00:00:00.000-05:00",
      "FirstPayDate": "2007-07-26T00:00:00.000-04:00",
      "PreviousPaymentDate": "2010-01-21T00:00:00.000-05:00",
      "DatedDate": "2007-04-26T00:00:00.000-04:00",
      "IsPayInKind": false,
      "UnadjustedPreviousCouponPayDat": "2010-01-21T00:00:00.000-05:00"
    },

    "version": 1,
    "id": 1615,
    "cloudstamp": "2010-12-04T11:46:20.739-05:00"
  }
 ]//end of results
}

Upvotes: 1

Views: 1872

Answers (1)

iamiddy
iamiddy

Reputation: 3073

Your JSON Data maps to ResultWrapper Pojo as shown below, with that you can now Iterate over results on ResultWrapper as you wish and be able to pick any entity/object eg.

Result result = resultWrapper.getResults().get(0)
      ExchangeListInfo exchangeListInfo = result.getExchangeListInfo();
      RedeemCall redeemCall = result.getRedeemCall(); //etc

Used JsonToPojo toool here and got the following , your main Pojo is ResultWrapper, have ommitted some imports for brevity

-----------------------------------com.example.ResultWrapper.java------

            package com.example;

            @JsonInclude(JsonInclude.Include.NON_NULL)
            @Generated("org.jsonschema2pojo")
            @JsonPropertyOrder({
            "code",
            "results"
            })
            public class ResultWrapper {

            @JsonProperty("code")
            private long code;
            @JsonProperty("results")
            private List<Result> results = new ArrayList<Result>();

            /**
            * 
            * @return
            * The code
            */
            @JsonProperty("code")
            public long getCode() {
            return code;
            }

            /**
            * 
            * @param code
            * The code
            */
            @JsonProperty("code")
            public void setCode(long code) {
            this.code = code;
            }

            /**
            * 
            * @return
            * The results
            */
            @JsonProperty("results")
            public List<Result> getResults() {
            return results;
            }

            /**
            * 
            * @param results
            * The results
            */
            @JsonProperty("results")
            public void setResults(List<Result> results) {
            this.results = results;
            }

            @JsonAnyGetter
            public Map<String, Object> getAdditionalProperties() {
            return this.additionalProperties;
            }

            @JsonAnySetter
            public void setAdditionalProperty(String name, Object value) {
            this.additionalProperties.put(name, value);
            }

            }

             -----------------------------------com.example.Result.java-----------------------------------

            package com.example;


            @JsonInclude(JsonInclude.Include.NON_NULL)
            @Generated("org.jsonschema2pojo")
            @JsonPropertyOrder({
            "_id",
            "ExchangeListInfo",
            "RedeemCall",
            "MsdInformation",
            "SecInformation",
            "ConversionBasic",
            "IncomeInformation",
            "version",
            "id",
            "cloudstamp"
            })
            public class Result {

            @JsonProperty("_id")
            private String Id;
            @JsonProperty("ExchangeListInfo")
            private com.example.ExchangeListInfo ExchangeListInfo;
            @JsonProperty("RedeemCall")
            private com.example.RedeemCall RedeemCall;
            @JsonProperty("MsdInformation")
            private com.example.MsdInformation MsdInformation;
            @JsonProperty("SecInformation")
            private com.example.SecInformation SecInformation;
            @JsonProperty("ConversionBasic")
            private com.example.ConversionBasic ConversionBasic;
            @JsonProperty("IncomeInformation")
            private com.example.IncomeInformation IncomeInformation;
            @JsonProperty("version")
            private long version;
            @JsonProperty("id")
            private long id;
            @JsonProperty("cloudstamp")
            private String cloudstamp;


            @JsonProperty("_id")
            public String getId() {
            return Id;
            }


            @JsonProperty("_id")
            public void setId(String Id) {
            this.Id = Id;
            }


            @JsonProperty("ExchangeListInfo")
            public com.example.ExchangeListInfo getExchangeListInfo() {
            return ExchangeListInfo;
            }



            @JsonProperty("ExchangeListInfo")
            public void setExchangeListInfo(com.example.ExchangeListInfo ExchangeListInfo) {
            this.ExchangeListInfo = ExchangeListInfo;
            }


            @JsonProperty("RedeemCall")
            public com.example.RedeemCall getRedeemCall() {
            return RedeemCall;
            }


            @JsonProperty("RedeemCall")
            public void setRedeemCall(com.example.RedeemCall RedeemCall) {
            this.RedeemCall = RedeemCall;
            }


            @JsonProperty("MsdInformation")
            public com.example.MsdInformation getMsdInformation() {
            return MsdInformation;
            }



            @JsonProperty("MsdInformation")
            public void setMsdInformation(com.example.MsdInformation MsdInformation) {
            this.MsdInformation = MsdInformation;
            }


            @JsonProperty("SecInformation")
            public com.example.SecInformation getSecInformation() {
            return SecInformation;
            }


            @JsonProperty("SecInformation")
            public void setSecInformation(com.example.SecInformation SecInformation) {
            this.SecInformation = SecInformation;
            }


            @JsonProperty("ConversionBasic")
            public com.example.ConversionBasic getConversionBasic() {
            return ConversionBasic;
            }


            @JsonProperty("ConversionBasic")
            public void setConversionBasic(com.example.ConversionBasic ConversionBasic) {
            this.ConversionBasic = ConversionBasic;
            }


            @JsonProperty("IncomeInformation")
            public com.example.IncomeInformation getIncomeInformation() {
            return IncomeInformation;
            }



            @JsonProperty("IncomeInformation")
            public void setIncomeInformation(com.example.IncomeInformation IncomeInformation) {
            this.IncomeInformation = IncomeInformation;
            }


            @JsonProperty("version")
            public long getVersion() {
            return version;
            }


            @JsonProperty("version")
            public void setVersion(long version) {
            this.version = version;
            }


            @JsonProperty("id")
            public long getId() {
            return id;
            }



            @JsonProperty("id")
            public void setId(long id) {
            this.id = id;
            }


            @JsonProperty("cloudstamp")
            public String getCloudstamp() {
            return cloudstamp;
            }


            @JsonProperty("cloudstamp")
            public void setCloudstamp(String cloudstamp) {
            this.cloudstamp = cloudstamp;
            }

            @JsonAnyGetter
            public Map<String, Object> getAdditionalProperties() {
            return this.additionalProperties;
            }

            @JsonAnySetter
            public void setAdditionalProperty(String name, Object value) {
            this.additionalProperties.put(name, value);
            }

            }

    -----------------------com.example.ConversionBasic.java-----------
            package com.example;
            @JsonInclude(JsonInclude.Include.NON_NULL)
            @Generated("org.jsonschema2pojo")
            @JsonPropertyOrder({
            "ConversionMandatoryFlag"
            })
            public class ConversionBasic {

            @JsonProperty("ConversionMandatoryFlag")
            private boolean ConversionMandatoryFlag;


            @JsonProperty("ConversionMandatoryFlag")
            public boolean isConversionMandatoryFlag() {
            return ConversionMandatoryFlag;
            }

            /**
            * 
            * @param ConversionMandatoryFlag
            * The ConversionMandatoryFlag
            */
            @JsonProperty("ConversionMandatoryFlag")
            public void setConversionMandatoryFlag(boolean ConversionMandatoryFlag) {
            this.ConversionMandatoryFlag = ConversionMandatoryFlag;
            }

            @JsonAnyGetter
            public Map<String, Object> getAdditionalProperties() {
            return this.additionalProperties;
            }

            @JsonAnySetter
            public void setAdditionalProperty(String name, Object value) {
            this.additionalProperties.put(name, value);
            }

            }
            -----------------------------------com.example.ExchangeListInfo.java----

            package com.example;


            @JsonInclude(JsonInclude.Include.NON_NULL)
            @Generated("org.jsonschema2pojo")
            @JsonPropertyOrder({
            "InstrumentListingLevel",
            "SettlementCurrency"
            })
            public class ExchangeListInfo {

            @JsonProperty("InstrumentListingLevel")
            private String InstrumentListingLevel;
            @JsonProperty("SettlementCurrency")
            private String SettlementCurrency;


            @JsonProperty("InstrumentListingLevel")
            public String getInstrumentListingLevel() {
            return InstrumentListingLevel;
            }


            @JsonProperty("InstrumentListingLevel")
            public void setInstrumentListingLevel(String InstrumentListingLevel) {
            this.InstrumentListingLevel = InstrumentListingLevel;
            }


            @JsonProperty("SettlementCurrency")
            public String getSettlementCurrency() {
            return SettlementCurrency;
            }


            @JsonProperty("SettlementCurrency")
            public void setSettlementCurrency(String SettlementCurrency) {
            this.SettlementCurrency = SettlementCurrency;
            }

            @JsonAnyGetter
            public Map<String, Object> getAdditionalProperties() {
            return this.additionalProperties;
            }

            @JsonAnySetter
            public void setAdditionalProperty(String name, Object value) {
            this.additionalProperties.put(name, value);
            }

            }
            -----------------------------------com.example.IncomeInformation.java-------

            package com.example;


            @JsonInclude(JsonInclude.Include.NON_NULL)
            @Generated("org.jsonschema2pojo")
            @JsonPropertyOrder({
            "AccrualMethod",
            "NextPayDate",
            "CouponDividendRate",
            "CouponDividendType",
            "CouponDividendCurrency",
            "CouponDividendFrequency",
            "LastPayDate",
            "FirstPayDate",
            "PreviousPaymentDate",
            "DatedDate",
            "IsPayInKind",
            "UnadjustedPreviousCouponPayDat"
            })
            public class IncomeInformation {

            @JsonProperty("AccrualMethod")
            private String AccrualMethod;
            @JsonProperty("NextPayDate")
            private String NextPayDate;
            @JsonProperty("CouponDividendRate")
            private double CouponDividendRate;
            @JsonProperty("CouponDividendType")
            private String CouponDividendType;
            @JsonProperty("CouponDividendCurrency")
            private String CouponDividendCurrency;
            @JsonProperty("CouponDividendFrequency")
            private long CouponDividendFrequency;
            @JsonProperty("LastPayDate")
            private String LastPayDate;
            @JsonProperty("FirstPayDate")
            private String FirstPayDate;
            @JsonProperty("PreviousPaymentDate")
            private String PreviousPaymentDate;
            @JsonProperty("DatedDate")
            private String DatedDate;
            @JsonProperty("IsPayInKind")
            private boolean IsPayInKind;
            @JsonProperty("UnadjustedPreviousCouponPayDat")
            private String UnadjustedPreviousCouponPayDat;
            @JsonProperty("AccrualMethod")
            public String getAccrualMethod() {
            return AccrualMethod;
            }


            @JsonProperty("AccrualMethod")
            public void setAccrualMethod(String AccrualMethod) {
            this.AccrualMethod = AccrualMethod;
            }

            @JsonProperty("NextPayDate")
            public String getNextPayDate() {
            return NextPayDate;
            }


            @JsonProperty("NextPayDate")
            public void setNextPayDate(String NextPayDate) {
            this.NextPayDate = NextPayDate;
            }
            @JsonProperty("CouponDividendRate")
            public double getCouponDividendRate() {
            return CouponDividendRate;
            }
            @JsonProperty("CouponDividendRate")
            public void setCouponDividendRate(double CouponDividendRate) {
            this.CouponDividendRate = CouponDividendRate;
            }

            @JsonProperty("CouponDividendType")
            public String getCouponDividendType() {
            return CouponDividendType;
            }

            @JsonProperty("CouponDividendType")
            public void setCouponDividendType(String CouponDividendType) {
            this.CouponDividendType = CouponDividendType;
            }

            @JsonProperty("CouponDividendCurrency")
            public String getCouponDividendCurrency() {
            return CouponDividendCurrency;
            }
            @JsonProperty("CouponDividendCurrency")
            public void setCouponDividendCurrency(String CouponDividendCurrency) {
            this.CouponDividendCurrency = CouponDividendCurrency;
            }

            @JsonProperty("CouponDividendFrequency")
            public long getCouponDividendFrequency() {
            return CouponDividendFrequency;
            }

            @JsonProperty("CouponDividendFrequency")
            public void setCouponDividendFrequency(long CouponDividendFrequency) {
            this.CouponDividendFrequency = CouponDividendFrequency;
            }

            @JsonProperty("LastPayDate")
            public String getLastPayDate() {
            return LastPayDate;
            }

            @JsonProperty("LastPayDate")
            public void setLastPayDate(String LastPayDate) {
            this.LastPayDate = LastPayDate;
            }

            @JsonProperty("FirstPayDate")
            public String getFirstPayDate() {
            return FirstPayDate;
            }

            @JsonProperty("FirstPayDate")
            public void setFirstPayDate(String FirstPayDate) {
            this.FirstPayDate = FirstPayDate;
            }

            @JsonProperty("PreviousPaymentDate")
            public String getPreviousPaymentDate() {
            return PreviousPaymentDate;
            }

            @JsonProperty("PreviousPaymentDate")
            public void setPreviousPaymentDate(String PreviousPaymentDate) {
            this.PreviousPaymentDate = PreviousPaymentDate;
            }

            @JsonProperty("DatedDate")
            public String getDatedDate() {
            return DatedDate;
            }

            @JsonProperty("DatedDate")
            public void setDatedDate(String DatedDate) {
            this.DatedDate = DatedDate;
            }


            @JsonProperty("IsPayInKind")
            public boolean isIsPayInKind() {
            return IsPayInKind;
            }


            @JsonProperty("IsPayInKind")
            public void setIsPayInKind(boolean IsPayInKind) {
            this.IsPayInKind = IsPayInKind;
            }


            @JsonProperty("UnadjustedPreviousCouponPayDat")
            public String getUnadjustedPreviousCouponPayDat() {
            return UnadjustedPreviousCouponPayDat;
            }


            @JsonProperty("UnadjustedPreviousCouponPayDat")
            public void setUnadjustedPreviousCouponPayDat(String UnadjustedPreviousCouponPayDat) {
            this.UnadjustedPreviousCouponPayDat = UnadjustedPreviousCouponPayDat;
            }

            @JsonAnyGetter
            public Map<String, Object> getAdditionalProperties() {
            return this.additionalProperties;
            }

            @JsonAnySetter
            public void setAdditionalProperty(String name, Object value) {
            this.additionalProperties.put(name, value);
            }

            }
            -----------------------------------com.example.MsdInformation.java------

            package com.example;

            @JsonInclude(JsonInclude.Include.NON_NULL)
            @Generated("org.jsonschema2pojo")
            @JsonPropertyOrder({
            "MSDCallPutFlag",
            "MSDMinimumDenomination",
            "MSDCouponFrequency",
            "MSDBondForm",
            "MSDSovDebtFlag",
            "MSDDatedDt",
            "MSDMaturityDt"
            })
            public class MsdInformation {

            @JsonProperty("MSDCallPutFlag")
            private String MSDCallPutFlag;
            @JsonProperty("MSDMinimumDenomination")
            private double MSDMinimumDenomination;
            @JsonProperty("MSDCouponFrequency")
            private String MSDCouponFrequency;
            @JsonProperty("MSDBondForm")
            private String MSDBondForm;
            @JsonProperty("MSDSovDebtFlag")
            private boolean MSDSovDebtFlag;
            @JsonProperty("MSDDatedDt")
            private String MSDDatedDt;
            @JsonProperty("MSDMaturityDt")
            private String MSDMaturityDt;
            @JsonIgnore
            private Map<String, Object> additionalProperties = new HashMap<String, Object>();


            @JsonProperty("MSDCallPutFlag")
            public String getMSDCallPutFlag() {
            return MSDCallPutFlag;
            }

            @JsonProperty("MSDCallPutFlag")
            public void setMSDCallPutFlag(String MSDCallPutFlag) {
            this.MSDCallPutFlag = MSDCallPutFlag;
            }


            @JsonProperty("MSDMinimumDenomination")
            public double getMSDMinimumDenomination() {
            return MSDMinimumDenomination;
            }


            @JsonProperty("MSDMinimumDenomination")
            public void setMSDMinimumDenomination(double MSDMinimumDenomination) {
            this.MSDMinimumDenomination = MSDMinimumDenomination;
            }


            @JsonProperty("MSDCouponFrequency")
            public String getMSDCouponFrequency() {
            return MSDCouponFrequency;
            }

            @JsonProperty("MSDCouponFrequency")
            public void setMSDCouponFrequency(String MSDCouponFrequency) {
            this.MSDCouponFrequency = MSDCouponFrequency;
            }


            @JsonProperty("MSDBondForm")
            public String getMSDBondForm() {
            return MSDBondForm;
            }


            @JsonProperty("MSDBondForm")
            public void setMSDBondForm(String MSDBondForm) {
            this.MSDBondForm = MSDBondForm;
            }


            @JsonProperty("MSDSovDebtFlag")
            public boolean isMSDSovDebtFlag() {
            return MSDSovDebtFlag;
            }


            @JsonProperty("MSDSovDebtFlag")
            public void setMSDSovDebtFlag(boolean MSDSovDebtFlag) {
            this.MSDSovDebtFlag = MSDSovDebtFlag;
            }


            @JsonProperty("MSDDatedDt")
            public String getMSDDatedDt() {
            return MSDDatedDt;
            }


            @JsonProperty("MSDDatedDt")
            public void setMSDDatedDt(String MSDDatedDt) {
            this.MSDDatedDt = MSDDatedDt;
            }


            @JsonProperty("MSDMaturityDt")
            public String getMSDMaturityDt() {
            return MSDMaturityDt;
            }


            @JsonProperty("MSDMaturityDt")
            public void setMSDMaturityDt(String MSDMaturityDt) {
            this.MSDMaturityDt = MSDMaturityDt;
            }

            @JsonAnyGetter
            public Map<String, Object> getAdditionalProperties() {
            return this.additionalProperties;
            }

            @JsonAnySetter
            public void setAdditionalProperty(String name, Object value) {
            this.additionalProperties.put(name, value);
            }

            }
            -----------------------------------com.example.RedeemCall.java----------

            package com.example;

            @JsonInclude(JsonInclude.Include.NON_NULL)
            @Generated("org.jsonschema2pojo")
            @JsonPropertyOrder({
            "IsMWholeCall"
            })
            public class RedeemCall {

            @JsonProperty("IsMWholeCall")
            private boolean IsMWholeCall;
            @JsonIgnore
            private Map<String, Object> additionalProperties = new HashMap<String, Object>();


            @JsonProperty("IsMWholeCall")
            public boolean isIsMWholeCall() {
            return IsMWholeCall;
            }


            @JsonProperty("IsMWholeCall")
            public void setIsMWholeCall(boolean IsMWholeCall) {
            this.IsMWholeCall = IsMWholeCall;
            }

            @JsonAnyGetter
            public Map<String, Object> getAdditionalProperties() {
            return this.additionalProperties;
            }

            @JsonAnySetter
            public void setAdditionalProperty(String name, Object value) {
            this.additionalProperties.put(name, value);
            }

            }

            -----------------------------------com.example.SecInformation.java------

            package com.example;

            @JsonInclude(JsonInclude.Include.NON_NULL)
            @Generated("org.jsonschema2pojo")
            @JsonPropertyOrder({
            "PutFreq",
            "ItDivFreq",
            "ItBasDysTyp",
            "PuTyp"
            })
            public class SecInformation {

            @JsonProperty("PutFreq")
            private String PutFreq;
            @JsonProperty("ItDivFreq")
            private String ItDivFreq;
            @JsonProperty("ItBasDysTyp")
            private String ItBasDysTyp;
            @JsonProperty("PuTyp")
            private String PuTyp;
            @JsonIgnore
            private Map<String, Object> additionalProperties = new HashMap<String, Object>();


            @JsonProperty("PutFreq")
            public String getPutFreq() {
            return PutFreq;
            }


            @JsonProperty("PutFreq")
            public void setPutFreq(String PutFreq) {
            this.PutFreq = PutFreq;
            }


            @JsonProperty("ItDivFreq")
            public String getItDivFreq() {
            return ItDivFreq;
            }

            @JsonProperty("ItDivFreq")
            public void setItDivFreq(String ItDivFreq) {
            this.ItDivFreq = ItDivFreq;
            }


            @JsonProperty("ItBasDysTyp")
            public String getItBasDysTyp() {
            return ItBasDysTyp;
            }


            @JsonProperty("ItBasDysTyp")
            public void setItBasDysTyp(String ItBasDysTyp) {
            this.ItBasDysTyp = ItBasDysTyp;
            }


            @JsonProperty("PuTyp")
            public String getPuTyp() {
            return PuTyp;
            }

            @JsonProperty("PuTyp")
            public void setPuTyp(String PuTyp) {
            this.PuTyp = PuTyp;
            }

            @JsonAnyGetter
            public Map<String, Object> getAdditionalProperties() {
            return this.additionalProperties;
            }

            @JsonAnySetter
            public void setAdditionalProperty(String name, Object value) {
            this.additionalProperties.put(name, value);
            }

            }

You can also Use this tool offline: Maven plugin Gradle plugin Ant task CLI Java API

Upvotes: 2

Related Questions