Siddharth Siddharth - 1 month ago 7
Java Question

How to change the json response fields to user defined fields in java?

I have the following json response:

{
"Labels": {
"com.prop.vendor": "Acme",
"com.example.license": "GPL",
"com.example.version": "1.0"
}
}


Now I want to process these values in my java class. The problem I have is for fields like com.example.version, pojo is being generated using the same as data types. Like,

private String com.example.version


which gives me compile-time errors.
So, I want to create a field "String version" which points to "com.example.version"
And I have tried using @JsonProperty and @JsonSetter.

Labels.java

public class Labels
{
@JsonProperty("com.example.version")
private String version;

@JsonProperty("com.example.vendor")
private String vendor;

@JsonProperty("com.example.license")
private String license;

public String getVersion()
{
return version;
}

public void setVersion(String version)
{
this.version = version;
}

public String getVendor()
{
return vendor;
}

public void setVendor(String vendor)
{
this.vendor = vendor;
}

public String getLicense()
{
return license;
}

public void setLicense(String license)
{
this.license = license;
}

@Override
public String toString()
{
return "ClassPojo [com.example.version = " + version + ", com.example.vendor = " + vendor + ", com.example.license = "
+ license + "]";
}
}


But everytime I am getting this error:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Labels" (class com.csc.agility.adapters.service.docker.pojo.Labels), not marked as ignorable (3 known properties: "com.example.license", "com.example.vendor", "com.example.version"])
at [Source: {
"Labels": {
"com.example.vendor": "Acme",
"com.example.license": "GPL",
"com.example.version": "1.0"
}
}; line: 2, column: 12] (through reference chain: com.csc.agility.adapters.service.docker.pojo.Labels["Labels"])


How I am using it:

ObjectMapper objMap = new ObjectMapper();
String jsonInString =
"{\n\"Labels\": {\n \"com.example.vendor\": \"Acme\",\n \"com.example.license\": \"GPL\",\n \"com.example.version\": \"1.0\"\n }\n}";

Label label = objMap.readValue(jsonInString, Label.class);
String licence = label.getLicense();


Can anyone point me to the correct json annotation to use or any other way to achieve it?

Answer

You should configure your mapper with

mapper.configure(Feature.UNWRAP_ROOT_VALUE, true); (for version 1.9)

or

mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true); (for version 2 and above).

I have tried your json with above properties and everything works fine:

String data = "{\"Labels\": {\"com.example.vendor\": \"Acme\",\"com.example.license\": \"GPL\",\"com.example.version\": \"1.0\"}}";

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
Labels labels = mapper.readValue(data, Labels.class);
System.out.println(labels);

Note: In Json String, I have changed com.prop.vendor to com.example.vendor to make it workable.