Home > Back-end >  How to map a complex JSON with POJO
How to map a complex JSON with POJO

Time:07-12

I'm having some trouble with how to map a JSON correctly. I have to save this incoming JSON in my DB:

{
"request": {
   "Target": "Affiliate_Offer",
   "Format": "json",
   "Service": "HasOffers",
   "Version": "2",
   "api_key": "3225235c56334584b1360d",
   "Method": "findAll",
   "contain": [
      "Country"
  ]
},
"response": {
  "status": 1,
  "httpStatus": 200,
  "data": {
      "18292": {
          "Offer": {
              "id": "18292",
              "name": "247 Profit Formula",    
              "approval_status": "approved"
          },
          "Country": {
              "US": {
                  "id": "840",
                  "code": "US",
                  "name": "United States",
                  "regions": {
                    "4": {
                      "id": "4",
                      "country_code": "US",
                      "country_code_3c": "USA",
                      "code": "AR",
                      "name": "Arkansas"
                   },
                   "5": {
                     "id": "5",
                     "country_code": "US",
                     "country_code_3c": "USA",
                     "code": "CA",
                     "name": "California"
                   },
                  "10": {
                     "id": "10",
                     "country_code": "US",
                     "country_code_3c": "USA",
                     "code": "FL",
                     "name": "Florida"
                  }
              },
              "CA": {
                 "id": "124",
                 "code": "CA",
                 "name": "Canada",
                 "regions": []
              }
          }
      },
      "17823": {
          "Offer": {
              "id": "17823",
              "name": "American Career Guide",              
              "approval_status": null
          },
          "Country": {
               "Country": {
      "US": {
        "id": "840",
        "code": "US",
        "name": "United States",
        "regions": []
      },
      "UK": {
        "id": "826",
        "code": "UK",
        "name": "United Kingdom",
        "regions": []
      },
      "AU": {
        "id": "36",
        "code": "AU",
        "name": "Australia",
        "regions": []
      },
      "CA": {
        "id": "124",
        "code": "CA",
        "name": "Canada",
        "regions": []
      },
      "NZ": {
        "id": "554",
        "code": "NZ",
        "name": "New Zealand",
        "regions": []
      }
    }
   }
 },

The Offer structure is already mapped, but I'm having some trouble with the Country. The OfferMapper:

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class OfferMapper {
   OfferRequest request;
   OfferResponse response;
}

The OfferResponse:

@Getter @Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class OfferResponse {

   @JsonProperty("status")
   private int status;
   @JsonProperty("httpStatus")
   private int httpStatus;
   @JsonProperty("data")
   private Map<String, OfferData> data;
}

Here is the structure that I'm having trouble. The OfferData:

@Getter @Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class OfferData {
   @JsonProperty("Offer")
   Offer offer;

   @JsonProperty("Country")
   Country country;
}

I tried to put in a map like this Map<Country, String> countries; and List<Map<Country, String>> countries; no luck so far to arrange this part with a POJO:

"Country": {
          "US": {
              "id": "840",
              "code": "US",
              "name": "United States",
              "regions": []
          }
      }

CodePudding user response:

"Country": {
          "US": {
              "id": "840",
              "code": "US",
              "name": "United States",
              "regions": []
          }
      }

It is equivalent to receiving an object of Country that has an object of US.

Structure of US

public class US
{
private String id;
private String code;
private String name;
private List<String> regions; //if regions is gonna be list of strings

//getters and setters
}

CodePudding user response:

Your Json Country is having letter C as capital hence it might be the issue that Jackson is not recognizing it. Check Java Naming conventions and other docs for more information.

class Data {
    private String id;
    private String code;
    private String name;
    private List<String> regions;
}

class CountryData {
    public Map<String, Data> getCountry() {
        return Country;
    }

    public void setCountry(Map<String, Data> Country) {
        Country = Country;
    }
//To resolve the error you need to add the below tag and define your class accordingly.
    @JsonProperty("Country")
    private Map<String, Data> Country;
}

public class POJO {

    public static void main(String[] args) throws JsonProcessingException {
        String countryjson = "{\n"  
                "  \"Country\": {\n"  
                "    \"US\": {\n"  
                "      \"id\": \"840\",\n"  
                "      \"code\": \"US\",\n"  
                "      \"name\": \"United States\",\n"  
                "      \"regions\": []\n"  
                "    },\n"  
                "    \"UK\": {\n"  
                "      \"id\": \"826\",\n"  
                "      \"code\": \"UK\",\n"  
                "      \"name\": \"United Kingdom\",\n"  
                "      \"regions\": []\n"  
                "    },\n"  
                "    \"AU\": {\n"  
                "      \"id\": \"36\",\n"  
                "      \"code\": \"AU\",\n"  
                "      \"name\": \"Australia\",\n"  
                "      \"regions\": []\n"  
                "    },\n"  
                "    \"CA\": {\n"  
                "      \"id\": \"124\",\n"  
                "      \"code\": \"CA\",\n"  
                "      \"name\": \"Canada\",\n"  
                "      \"regions\": []\n"  
                "    },\n"  
                "    \"NZ\": {\n"  
                "      \"id\": \"554\",\n"  
                "      \"code\": \"NZ\",\n"  
                "      \"name\": \"New Zealand\",\n"  
                "      \"regions\": []\n"  
                "    }\n"  
                "  }\n"  
                "}";
        ObjectMapper objectMapper = new ObjectMapper();
        CountryData country = objectMapper.readValue(countryjson, CountryData.class);
        System.out.println(country.toString());
    }
}

It should work, Add @JsonProperty Tag in you POJO definition.

  • Related