Niroshan Niroshan - 4 months ago 10
JSON Question

Mapping JSON String to a POJO with Jackson gives null values

I am trying to read some values under "properties" of following JSON string to a POJO. But all I get is null values.

{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
144.9798,
-37.743
]
},
"properties": {
"PFI": "51351644",
"EZI_ADD": "581 BELL STREET COBURG 3058",
"ROAD_NAME": "BELL",
"ROAD_TYPE": "STREET",
"LOCALITY": "COBURG",
"LGA_CODE": "316",
"STATE": "VIC",
"POSTCODE": "3058",
"ADD_CLASS": "S"
},
"id": "ADDRESS.581"
}


My POJO class

@JsonIgnoreProperties(ignoreUnknown = true)
class Property {

public Property(){}

private String EZI_ADD; // e.g., "14 FAIRWAY COURT BUNDOORA 3083"
private String STATE; // e.g., "VIC"
private String POSTCODE; // e.g., "3083"
private String LGA_CODE; // e.g., 373
private String LOCALITY; // e.g., "BUNDOORA"
private String ADD_CLASS; // e.g., "S", or "M"
private String SA1_7DIG11 = ""; // SA1 code e.g., "2120241"

public String getEZI_ADD() {
return EZI_ADD;
}

@JsonProperty("EZI_ADD")
public void setEZI_ADD(String eZI_ADD) {
EZI_ADD = eZI_ADD;
}

public String getSTATE() {
return STATE;
}

@JsonProperty("STATE")
public void setSTATE(String sTATE) {
STATE = sTATE;
}

public String getPOSTCODE() {
return POSTCODE;
}

@JsonProperty("POSTCODE")
public void setPOSTCODE(String pOSTCODE) {
POSTCODE = pOSTCODE;
}

public String getLGA_CODE() {
return LGA_CODE;
}

@JsonProperty("LGA_CODE")
public void setLGA_CODE(String lGA_CODE) {
LGA_CODE = lGA_CODE;
}

public String getLOCALITY() {
return LOCALITY;
}

@JsonProperty("LOCALITY")
public void setLOCALITY(String lOCALITY) {
LOCALITY = lOCALITY;
}

public String getADD_CLASS() {
return ADD_CLASS;
}

@JsonProperty("ADD_CLASS")
public void setADD_CLASS(String aDD_CLASS) {
ADD_CLASS = aDD_CLASS;
}

public String getSA1_7DIG11() {
return SA1_7DIG11;
}

@JsonProperty("SA1_7DIG11")
public void setSA1_7DIG11(String sA1_7DIG11) {
SA1_7DIG11 = sA1_7DIG11;
}
}


Conversion code is as follows

//Above json string
String jsonString = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[144.9798,-37.743]},\"properties\":{\"PFI\":\"51351644\",\"EZI_ADD\":\"581 BELL STREET COBURG 3058\",\"ROAD_NAME\":\"BELL\",\"ROAD_TYPE\":\"STREET\",\"LOCALITY\":\"COBURG\",\"LGA_CODE\":\"316\",\"STATE\":\"VIC\",\"POSTCODE\":\"3058\",\"ADD_CLASS\":\"S\"},\"id\":\"ADDRESS.581\"}";
ObjectMapper mapper = new ObjectMapper();
Property properties = mapper.readValue(jsonString, Property.class);


Output:

{
"properties": {
"EZI_ADD": null,
"STATE": null,
"POSTCODE": null,
"LGA_CODE": null,
"LOCALITY": null,
"ADD_CLASS": null,
"SA1_7DIG11": ""
}
}

Answer

The JSON String you're sending does not match the Property class. Add a wrapper class, e.g. something like this:

public class Feature {

    private String type;
    private String id;
    private Property property;

    // getters and setters
}

Then you can send the request and the JSON String will be parsed to your object:

{
  "type": "feature",
  "id": "test",
  "property": {
    "PFI": "51351644",
    "EZI_ADD": "581 BELL STREET COBURG 3058",
    "ROAD_NAME": "BELL",
    "ROAD_TYPE": "STREET",
    "LOCALITY": "COBURG",
    "LGA_CODE": "316",
    "STATE": "VIC",
    "POSTCODE": "3058",
    "ADD_CLASS": "S"
  }
}