Jackson ObjectMapper not able to parse string as hashmap

65
October 15, 2021, at 7:20 PM

I want to parse a Json String to Map<String, Object> and use the same in the existing application.

import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
public class ObjectMapperDemo {
    public static String applicantJson = "{\\\"lead_id\\\":null,\\\"finder_number\\\":null,\\\"applicant_index\\\":0,\\\"applicant_id\\\":100314,\\\"loan_application_id\\\":3000312,\\\"person\\\":{\\\"lastName[PROTECTED],\\\"firstName\\\":\\\"Cathy\\\",\\\"maternalName[PROTECTED],\\\"middleName\\\":\\\"Kimberly\\\",\\\"dateOfBirth[PROTECTED],\\\"ssn[PROTECTED],\\\"itin[PROTECTED],\\\"last4Ssn[PROTECTED],\\\"last4Itin[PROTECTED],\\\"oktaId\\\":null,\\\"suffix\\\":\\\"Sr\\\",\\\"title\\\":null,\\\"clientId\\\":null,\\\"sipClientId\\\":null,\\\"customerId\\\":null,\\\"inUSSince\\\":null},\\\"email\\\":[{\\\"home\\\":\\\"SwNjh67784@oportun.com\\\"}],\\\"address[PROTECTED],\\\"street[PROTECTED],\\\"street[PROTECTED],\\\"city[PROTECTED],\\\"state\\\":\\\"FL\\\",\\\"postalCode\\\":\\\"33144\\\"}],\\\"identification[PROTECTED],\\\"number[PROTECTED],\\\"expirationDate\\\":\\\"2023-02-02T00:00:00.000Z\\\",\\\"dateOfBirth[PROTECTED],\\\"firstName\\\":\\\"Cathy\\\",\\\"middleName\\\":\\\"Kimberly\\\",\\\"lastName[PROTECTED],\\\"maternalName[PROTECTED],\\\"suffix\\\":\\\"Sr\\\",\\\"issuingCountry\\\":\\\"US\\\",\\\"issuingState\\\":\\\"CA\\\",\\\"issuingCity[PROTECTED],\\\"issueDate\\\":null,\\\"street[PROTECTED],\\\"street[PROTECTED],\\\"postalCode\\\":\\\"94063\\\",\\\"city[PROTECTED],\\\"state\\\":\\\"CA\\\",\\\"country\\\":null}],\\\"phones\\\":[{\\\"phoneNumber\\\":\\\"689-165-2363\\\",\\\"phoneType\\\":\\\"HM\\\"},{\\\"phoneNumber\\\":\\\"332-621-7769\\\",\\\"phoneType\\\":\\\"CL\\\"}]}";
    public static Map<String, Object> ConvertValueFromObject(Object obj) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
        objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        return objectMapper.convertValue(obj, Map.class);
    }
    public static void main(String[] args) {
        
        Map<String, Object> testMap = ConvertValueFromObject(applicantJson);
        System.out.println(testMap.keySet());
    }
}

The stacktrace thrown is as follows:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot construct instance of `java.util.LinkedHashMap` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{\"lead_id\":null,\"finder_number\":null,\"applicant_index\":0,\"applicant_id\":100314,\"loan_application_id\":3000312,\"person\":{\"lastName[PROTECTED],\"firstName\":\"Cathy\",\"maternalName[PROTECTED],\"middleName\":\"Kimberly\",\"dateOfBirth[PROTECTED],\"ssn[PROTECTED],\"itin[PROTECTED],\"last4Ssn[PROTECTED],\"last4Itin[PROTECTED],\"oktaId\":null,\"suffix\":\"Sr\",\"title\":null,\"clientId\":null,\"sipClientId\":null,\"customerId\":null,\"inUSSince\":null},\"email\":[{\"home\":\"SwNjh67784@oportun.com\"}],\"address[PROTECTED],\"street[PROTECTED],\"street[PROTECTED],\"city[PROTECTED],\"state\":\"FL\",\"postalCode\":\"33144\"}],\"identification[PROTECTED],\"number[PROTECTED],\"expirationDate\":\"2023-02-02T00:00:00.000Z\",\"dateOfBirth[PROTECTED],\"firstName\":\"Cathy\",\"middleName\":\"Kimberly\",\"lastName[PROTECTED],\"maternalName[PROTECTED],\"suffix\":\"Sr\",\"issuingCountry\":\"US\",\"issuingState\":\"CA\",\"issuingCity[PROTECTED],\"issueDate\":null,\"street[PROTECTED],\"street[PROTECTED],\"postalCode\":\"94063\",\"city[PROTECTED],\"state\":\"CA\",\"country\":null}],\"phones\":[{\"phoneNumber\":\"689-165-2363\",\"phoneType\":\"HM\"},{\"phoneNumber\":\"332-621-7769\",\"phoneType\":\"CL\"}]}')
 at [Source: UNKNOWN; byte offset: #UNKNOWN]
    at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:4394)
    at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:4325)
    at ObjectMapperDemo.ConvertValueFromObject(ObjectMapperDemo.java:19)
    at ObjectMapperDemo.main(ObjectMapperDemo.java:25)
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `java.util.LinkedHashMap` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{\"lead_id\":null,\"finder_number\":null,\"applicant_index\":0,\"applicant_id\":100314,\"loan_application_id\":3000312,\"person\":{\"lastName[PROTECTED],\"firstName\":\"Cathy\",\"maternalName[PROTECTED],\"middleName\":\"Kimberly\",\"dateOfBirth[PROTECTED],\"ssn[PROTECTED],\"itin[PROTECTED],\"last4Ssn[PROTECTED],\"last4Itin[PROTECTED],\"oktaId\":null,\"suffix\":\"Sr\",\"title\":null,\"clientId\":null,\"sipClientId\":null,\"customerId\":null,\"inUSSince\":null},\"email\":[{\"home\":\"SwNjh67784@oportun.com\"}],\"address[PROTECTED],\"street[PROTECTED],\"street[PROTECTED],\"city[PROTECTED],\"state\":\"FL\",\"postalCode\":\"33144\"}],\"identification[PROTECTED],\"number[PROTECTED],\"expirationDate\":\"2023-02-02T00:00:00.000Z\",\"dateOfBirth[PROTECTED],\"firstName\":\"Cathy\",\"middleName\":\"Kimberly\",\"lastName[PROTECTED],\"maternalName[PROTECTED],\"suffix\":\"Sr\",\"issuingCountry\":\"US\",\"issuingState\":\"CA\",\"issuingCity[PROTECTED],\"issueDate\":null,\"street[PROTECTED],\"street[PROTECTED],\"postalCode\":\"94063\",\"city[PROTECTED],\"state\":\"CA\",\"country\":null}],\"phones\":[{\"phoneNumber\":\"689-165-2363\",\"phoneType\":\"HM\"},{\"phoneNumber\":\"332-621-7769\",\"phoneType\":\"CL\"}]}')
 at [Source: UNKNOWN; byte offset: #UNKNOWN]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1728)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1353)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer._deserializeFromString(StdDeserializer.java:311)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:444)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:32)
    at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:4389)
    ... 3 more

I even tried by passing the String as an Object

Map<String, Object> testMap = ConvertValueFromObject((Object) applicantJson);
READ ALSO
How to convert wasm back to C++ if I also have the original code

How to convert wasm back to C++ if I also have the original code

I'm not the best at explaining this stuff but here I go

66
Module not found: Error: Can&#39;t resolve &#39;fsevents&#39; in &#39;/var/www/html/node_modules/chokidar/lib&#39; [closed]

Module not found: Error: Can't resolve 'fsevents' in '/var/www/html/node_modules/chokidar/lib' [closed]

Want to improve this question? Add details and clarify the problem by editing this post

33
How to Inject JavaScript in Web Page to Automatically Hit Refresh Button

How to Inject JavaScript in Web Page to Automatically Hit Refresh Button

I want to update the given dataset every 1 minideally, so far I've understood the best scenario is either every 1h or every 15min from Power BI

37
How to handle duplicate dates in dates ranges in python

How to handle duplicate dates in dates ranges in python

Imagine an employee that has worked for different companies and you want to know how many months he worked for each companyThe problem is that he may have worked for two companies, overlapping some time (case of companies D and C)

53