3

I am trying to parse the following JSON to POJO, specifically the payload I want to extract as String[] or List of String without losing the JSON format.

{
  "payLoad": [
    {
      "id": 1,
      "userName": null,
      "arName": "A1",
      "areas": []
    },
    {
      "id": 2,
      "userName": "alpha2",
      "arName": "A2",
      "areas": []
    }
  ],
  "count": 2,
  "respCode": 200
}

Here is the POJO that I am using -

public class Response {

    @JsonProperty("count")
    private int totalCount;

    @JsonProperty("respCode")
    private int responseCode;

    @JsonProperty("payLoad")
    @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
    private String[] transactionsList;

    public String[] getTransactionsList() {
        return transactionsList;
    }

    public void setTransactionsList(String[] transactionsList) {
        this.transactionsList = transactionsList;
    }
..
}

This is method I am using with springboot to parse it automatically to

public void transactionsReceived() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    Response responseRcvd = objectMapper.readValue(jsonString, Response.class); 
}

Here is an error I am getting -

    Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `[Ljava.lang.String;` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (String)"{"payLoad": [{"id": 1,"userName": null,"arName": "A1","areas": []},{"id": 2,"userName": "alpha2","arName": "A2","areas": []}],"count": 2,"respCode": 200}"; line: 1, column: 14] (through reference chain: com.example.demo.model.Response["payLoad"]->java.lang.Object[][0])
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1741)..
4
  • Please clarify. Do you want the raw JSON objects in payload's JSON array as as Java String values in the transactionsList array? Commented Jun 9, 2022 at 16:14
  • @SotiriosDelimanolis Yes. I need it as String array or List of String while preserving the JSON format. Commented Jun 9, 2022 at 16:34
  • @Vikrant I can't understand what content do you want to have in the transactionsList ? Something like: "1","null","A1", [all elements from the array], whatNext ? Why don't you create appropriate object that will be just from conversion this Json into object and then convert that object in something that you want. It is much easier to achieve some things in java Commented Jun 9, 2022 at 18:49
  • @f.trajkovski, My program is like splitter, I don't need to know the content(which may change anytime). So, I am trying to get List<String> or String[] as below - {"id": 1,"userName": null,"arName": "A1","areas": []} {"id": 2,"userName": "alpha2","arName": "A2","areas": []} Commented Jun 9, 2022 at 18:56

1 Answer 1

2

You can write custom deserializer:

  public class JsonObjectListDeserializer extends StdDeserializer<List<String>> {

    public JsonObjectListDeserializer() {
      super(List.class);
    }

    @Override
    public List<String> deserialize(JsonParser parser, DeserializationContext context) throws IOException, JacksonException {
      JsonNode node = parser.getCodec().readTree(parser);
      List<String> result = new ArrayList<>();
      if (node.isArray()) {
        for (JsonNode element : node) {
          result.add(element.toString());
        }
      } else if (node.isObject()) {
        result.add(node.toString());
      } else {
        //maybe nothing?
      }
      return result;
    }
  }

JsonNode.toString() returns json representation of the node, like this you revert the node back to json to save in list.

Then register the deserializer only for this particular field.

public static class Response {

    @JsonProperty("count")
    private int totalCount;

    @JsonProperty("respCode")
    private int responseCode;

    @JsonProperty("payLoad")
    @JsonDeserialize(using = JsonObjectListDeserializer.class)
    private List<String> transactionsList;

    //getters, setters, etc.
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.