I have this Json:
[
{
"Title": "New Moon in Sagittarius",
"eventStartDate": "01/12/2024",
"eventEndDate": "01/12/2024"
},
{
"Title": "Christmas",
"eventStartDate": "25/12/2024",
"eventEndDate": "25/12/2024"
},
{
"Title": "New Year's Eve",
"eventStartDate": "31/12/2024",
"eventEndDate": "31/12/2024"
}
]
but when I parse it:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setDateFormat(new StdDateFormat().withLenient(false));
// Convert JSON string to Wrapper Class
AstronomicalEventsWrapper wrapper = objectMapper.readValue
(jsonString, AstronomicalEventsWrapper.class);
I have this error:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `com.systicblanets.model.AstronomicalEventsWrapper` from Array value (token `JsonToken.START_ARRAY`)
at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1767)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1541)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1488)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeFromArray(BeanDeserializer.java:656)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:211)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:187)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4899)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3846)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3814)
at com.mysticplanets.schedulingtasks.CreateAstronomicalEventTask.sendMessage(CreateAstronomicalEventTask.java:60)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.springframework.scheduling.support.ScheduledMethodRunnable.runInternal(ScheduledMethodRunnable.java:130)
at org.springframework.scheduling.support.ScheduledMethodRunnable.lambda$run$2(ScheduledMethodRunnable.java:124)
at io.micrometer.observation.Observation.observe(Observation.java:499)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:124)
at org.springframework.scheduling.config.Task$OutcomeTrackingRunnable.run(Task.java:83)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)
at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:358)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1583)
and
import java.util.List;
public class AstronomicalEventsWrapper {
private List<AstronomicalEvent> astrologicalEvents;
public List<AstronomicalEvent> getAstronomicalEvents() {
return astrologicalEvents;
}
public void setAstronomicalEvents(List<AstronomicalEvent> astrologicalEvents) {
this.astrologicalEvents = astrologicalEvents;
}
@Override
public String toString() {
return "AstronomicalEventsWrapper{" +
"astrologicalEvents=" + astrologicalEvents +
'}';
}
}
according to Jackson documentation , INCLUDE_SOURCE_IN_LOCATION
is used when producing the exception stack trace, so it is not relevant to the actual parse error.
i believe the parse error is because the input is an array of objects and so the target of deserialization must be either an array or collection. even if your wrapper contains a collection, it is still an object, and the input must be an object, meaning start with '{'