Search code examples
javajsonjacksonmapper

Mapping JSON String to List of POJO gives null values


I'm getting json from rest api and I want to store the data in list of POJO. Below is the codefor the same:

public List<myObject> mapper(){

    String myObjectData= restClient.getAllOriginal("myObject");

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

    CollectionType typeReference =
            TypeFactory.defaultInstance().constructCollectionType(List.class, myObject.class);
    List<CommitmentPojo> resultDto = null;

    try
    {

         resultDto = objectMapper.readValue(myObjectData, typeReference);
        
    }
    catch (JsonParseException e)
    {
        e.printStackTrace();
    }
    catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return resultDto;
}

I've added FAIL_ON_UNKNOWN_PROPERTIES configuration as I've extra columns in json as compared to POJO and I can't change POJO(unless and until required) as I'll have to change many more things. I've added ACCEPT_SINGLE_VALUE_AS_ARRAY configuration for object mapper as I was facing exception in below line: (I suspect this is causing the issue now)

// [JACKSON-526]: implicit arrays from single values?
        if (!ctxt.isEnabled(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)) {
            throw ctxt.mappingException(_collectionType.getRawClass());
        }

This is from CollectionDeserializer.handleNonArray method.

Method which gets the string from rest api:

public  String getAllOriginal(String resourcePath) {
       // Objects.requireNonNull(this.baseUri, "target cannot be null");
        return this.client
                .target("http://comtsrvc.ny.qa.flx.nimbus.gs.com:3802/v2/")
                .path(resourcePath)
                .request(MediaType.APPLICATION_JSON_TYPE)
                .cookie("GSSSO", getCookie())
                .get()
                .readEntity(String.class);
    }

Below is my json:

{
  
  "myObject" : [ {
    "key" : {
      "srcSys" : "REPO_1",
      "srcSysRef" : "20200909_1911_1"
    },
    "productData" : {
      "id" : null,
      "number" : null,
      "isn" : null,
      "productId" : null,
      "productAdditionalData" : {
        "assetClassTree" : "UNCLASSIFIED",
        "description" : "UNCLASSIFIED",
        "productTypeData" : {
          "productType" : "UNCLASSIFIED",
          "productGroup" : "UNCLASSIFIED"
        }
      }
    },
    "state" : "OPEN",
    "type" : "01"
  }, {
    "key" : {
      "srcSys" : "REPO_2",
      "srcSysRef" : "20200403_3892_1"
    },
    "productData" : {
      "id" : "1",
      "number" : "11",
      "isn" : "null",
      "productId" : 1234,
      "productAdditionalData" : {
        "assetClassTree" : "xyz",
        "description" : "abc",
        "productTypeData" : {
          "productType" : "UNCLASSIFIED",
          "productGroup" : "UNCLASSIFIED"
        }
      }
    },
    "state" : "OPEN",
    "tradAcctType" : "01"
  } ]
  }

The issue is: all the values are null with the size of list as 1. Can you please tell me what is wrong with my code.


Solution

  • Try to deserialize it to a Map:

    import com.fasterxml.jackson.core.type.TypeReference;
    
    ...
    
    Map<String, List<MyObject>> root = mapper.readValue(jsonFile, new TypeReference<Map<String, List<MyObject>>>() {});
    List<MyObject> objects = root.get("myObject");
    

    So you do not need to create a new POJO for a root level. Map will also work.