Here is my RAW Json data :
{ht_missingConditions=null, employeeNumber=UMB1075962, firstName=Pete}
Note: there is no double quotes
My Java Class
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class MBIdClass {
private String ht_missingConditions;
private String name;
private String employeeNumber;
//All setter getters
}
My Utility to convert JSON
public static Map<String,String> test(String json) {
ObjectMapper om=new ObjectMapper();
om.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
om.configure(com.fasterxml.jackson.core.JsonParser.Feature.AUTO_CLOSE_SOURCE, true);
try {
return om.readValue(json, new TypeReference<HashMap<String,String>>(){});
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Earlier I tried with passing Generic class but now I am trying at least it parse and convert into HashMap
.
I am calling using below ways
Map<String, String> testObj = test(sanitizedcleanData);
I am ending up below error:
com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'UMB1075962': was expecting ('true', 'false' or 'null')
at [Source: (String)"{ht_missingConditions:null, employeeNumber:UMB1075962, firstName:Pete st"[truncated 1569 chars]; line: 1, column: 54]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:703)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2853)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1899)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextFieldName(ReaderBasedJsonParser.java:968)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:512)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:364)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:29)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3023)
at com.devsoftbd.palash.studentsinfo.utils.CommonUtils.test(CommonUtils.java:80)
at com.devsoftbd.palash.studentsinfo.StudentsInfoApplication.lambda$0(StudentsInfoApplication.java:55)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
at com.devsoftbd.palash.studentsinfo.StudentsInfoApplication.main(StudentsInfoApplication.java:21)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
I have tried to replace =
with :
but still no luck
I was referring below article : StackOverflow-Issue
The form of your JSON string is invalid, it looks more like Properies class in Java.
Therefore, you can transform your invalid JSON string by replacing some characters to meet the format of .properties
file, and then use Properties
to read it as follows:
String inputStr = "{ht_missingConditions=null, employeeNumber=UMB1075962, firstName=Pete}";
inputStr = inputStr.replace("{", "").replace("}", "").replace(",", "\r\n");
Properties properties = new Properties();
properties.load(new ByteArrayInputStream(inputStr.getBytes(StandardCharsets.UTF_8)));
System.out.println(properties.toString()); // {ht_missingConditions=null, firstName=Pete, employeeNumber=UMB1075962}
Or you can also convert it to a Map
and get similar result:
Map<String, String> propMap = properties.entrySet().stream()
.collect(Collectors.toMap(
e -> String.valueOf(e.getKey()),
e -> String.valueOf(e.getValue()),
(prev, next) -> next, HashMap::new
));
System.out.println(propMap.toString()); // {firstName=Pete, ht_missingConditions=null, employeeNumber=UMB1075962}