0


I'd like to parse a JSON Array using the native JsonParser available in Spring Boot:

String url="https://restservice.com";
RestTemplate restTemplate = new RestTemplate();
String resp = restTemplate.getForObject(url, String.class);

JsonParser springParser = JsonParserFactory.getJsonParser();
Map<String, Object> map = springParser.parseMap(resp);

That works if there's just one item in the Array. If multiple items are returned, an exception is thrown:

Caused by: org.springframework.boot.json.JsonParseException: Cannot parse JSON
    at org.springframework.boot.json.AbstractJsonParser.tryParse(AbstractJsonParser.java:60) ~[spring-boot-2.1.0.RELEASE.jar!/:2.1.0.RELEASE]
    at org.springframework.boot.json.JacksonJsonParser.parseMap(JacksonJsonParser.java:55) ~[spring-boot-2.1.0.RELEASE.jar!/:2.1.0.RELEASE]
    at com.example.samplewebapp.DemoApplication.lambda$demo$2(DemoApplication.java:50) [classes!/:0.0.1-SNAPSHOT]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.0.RELEASE.jar!/:2.1.0.RELEASE]
    ... 13 common frames omitted
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.LinkedHashMap` out of START_ARRAY token

What is the correct API to return a List of Map JSON objects?

3
  • 1
    Have you tried parseList? Commented Dec 26, 2018 at 17:11
  • 2
    Can you show an example of what the data you're trying to parse looks like? Commented Dec 26, 2018 at 17:27
  • 1
    This means your data is an array. Cannot deserialize instance of java.util.LinkedHashMap out of START_ARRAY token It would be easier to help you if you could provide the input and the expected output. stackoverflow.com/help/mcve Commented Dec 26, 2018 at 18:03

1 Answer 1

5

Showing the JSON you are trying to parse would be helpful for getting a good answer to your problem.

Anyway, if you are trying to get a list you should use the parseList() method instead. This required doing a cast from objects to maps, but worked fine for me otherwise. Here's a quick example:

String json = "[{\"key\":\"value1\"}, {\"key\":\"value2\"}]";
JsonParser springParser = JsonParserFactory.getJsonParser();
List<Object> list = springParser.parseList(json);

for(Object o : list) {
    if(o instanceof Map) {
        Map<String,Object> map = (Map<String,Object>) o;
        //do processing here
    }
}

However, I would suggest calling the desired parser (Jackson, gson, etc.) directly if you need more control over the process. Jackson provides a handy ObjectMapper class to help with this sort of thing and avoid messy type conversions.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, that was what I was looking for.
No problem! Keep in mind that the "native JsonParser" that Spring Boot uses is just the first parser it finds in your classpath as seen here. So, it's probably still worth checking out these other libraries so you can get a feel for how to use them later on when this simple parse method isn't enough.

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.