Pkumar
Pkumar

Reputation: 177

How to define the data model without POJOS for received JSON format?

I have to prepare the model for the response to be sent to model.

This is the sample JSON format that is received:

{
\\ rest of the model
"Model": {
    "name": "name_01",
    "description": "name_01",
    "features": {
      "name": "feature 01",
      "description": "feature 01"
    }
  }
\\ Rest of the model
}

Response to be sent to the fronEnd :

{
\\ rest of the model

"model_name":"name_01",
"feature_name":"feature_01"
}
\\ rest of the model

This is what I implemented so far:

Code:

@SuppressWarnings("unchecked")
    @JsonProperty("model")
    private void unpackNestedModel(Map<String,Object> model) {
        this.model_name = (String) model.get("name");
        Map<String, Object> model2 = (Map<String, Object>) model.get("features");
        this.feature_name = (String) model2.get("name");
    }

Is there a better way?

Upvotes: 0

Views: 513

Answers (2)

cassiomolin
cassiomolin

Reputation: 130917

Assuming you are using Jackson and your request payload is like this:

{
  "Model": {
    "name": "name_01",
    "description": "name_01",
    "features": {
      "name": "feature 01",
      "description": "feature 01"
    }
  }
}

You could have the following classes to represent it:

@Data
public class RequestData {

    @JsonProperty("Model")
    private Model model;
}
@Data
public class Model {

    private String name;
    private String description;
    private Features features;
}
@Data
public class Features {

    private String name;
    private String description;
}

If you enable the UNWRAP_ROOT_VALUE deserialization feature in your ObjectMapper, you could simply have:

@Data
@JsonRootName("Model")
public class Model {

    private String name;
    private String description;
    private Features features;
}
@Data
public class Features {

    private String name;
    private String description;
}

If your response payload is like:

{
  "model_name": "name_01",
  "feature_name": "feature_01"
}

You could have:

@Data
public class ResponseData {

    @JsonProperty("model_name")
    private String modelName;

    @JsonProperty("feature_name")
    private String featureName;
}

Upvotes: 1

Holinc
Holinc

Reputation: 723

You can parse the result by JSONObject, but suggest to operate json result through POJO Object.

@Test
public void test02() {
    String receivedJson = "{\"Model\":{\"name\":\"name_01\",\"description\":\"name_01\",\"features\":{\"name\":\"feature 01\",\"description\":\"feature 01\"}}}";
    JSONObject receivedObj = JSONObject.parseObject(receivedJson);

    JSONObject model = (JSONObject) receivedObj.get("Model");
    JSONObject features = (JSONObject) model.get("features");

    JSONObject responseObj = new JSONObject();
    responseObj.put("model_name", model.getString("name"));
    responseObj.put("feature_name", features.getString("name"));

    System.out.println(responseObj);
    //output in console
    //{"model_name":"name_01","feature_name":"feature 01"}
}

Upvotes: 2

Related Questions