How to map dynamic JSON in JAX-RS

Viewed 1246

I have to map a JSON to Java PoJos using JAX-RS (Resteasy as implementation). The problem is, that the JSON is dynamic. Look at this example:

{
  "typeCode": "SAMPLE",
  "data": [
    {
      "id": "COMMENTS",
      "answerValue": {
        "type": "YesNoAnswer",
        "value": true
      }
    },
    {
      "id": "CHOICE",
      "answerValue": {
        "type": "SelectListAnswer",
        "values": ["choice1", "choice2"]
      }
    }
  ]
}

The dynamic elements are in the data array. In principal every entry has an ID and an answerValue. But the answerValue is dynamic. Depending on his type he can have a single value (boolean, string, number an object) or an array of values.

How can I map this to my Java model?

2 Answers

I suggest you handle it by getting the answerValue node as a JsonNode type, and processing it manually to the Java type you need.

Something along the lines of the following:

class Data {
    public String typeCode;
    public List<Answer> data;
}

class Answer {
    public String id;

    public void setAnswerValue(JsonNode node) {
        String type = node.path("type").asText();

        switch (type) {
            case "YesNoAnswer" :
                boolean value = node.path("value").asBoolean();
                // TODO Handle
                break;
            case "SelectListAnswer" :
                JsonNode values = node.path("values");
                for (JsonNode v : values) {
                    String s = v.textValue();
                    // TODO Handle
                }

                break;
          }
    }
}

Your input can then be read with an ObjectMapper:

ObjectMapper om = new ObjectMapper();
Data data = om.readValue(input, Data.class);
Related