Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error com.fasterxml.jackson.core.JsonParseException: Unrecognized token

Trying to read a JSON file and serialize it to java object, I wrote a method:

public static PostPojo readFile(String titleFile){
    String pathJSONFile = "src/main/resources/"+titleFile+".json";
    ObjectMapper objectMapper = new ObjectMapper();

    try {
         objectMapper.readValue(pathJSONFile,PostPojo.class);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return postPojo;
}

but it produces an error:

    com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'src': was expecting (JSON 
    String, Number, Array, Object or token 'null', 'true' or 'false')
    at [Source: (String)"src/main/resources/ninetyNinthPost.json"; line: 1, column: 4]
    at utils.ApiUtils.readFile(ApiUtils.java:71)
    at ApiApplicationRequest.getValue(ApiApplicationRequest.java:31)

My JSON file from which values are calculated

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[ {
 "userId" : 10,
 "id" : 99,
 "title" : "temporibus sit alias delectus eligendi possimus magni",
 "body" : "quo deleniti praesentium dicta non quod\naut est 
 molestias\nmolestias et officia quis nihil\nitaque dolorem quia"
} ]

My java object class

public class PostPojo {

private int userId;
private int id;
private String title;
private String body;
public PostPojo() {
}

public PostPojo(int userId, int id, String title, String body) {
    this.userId = userId;
    this.id = id;
    this.title = title;
    this.body = body;
}

public int getUserId() {
    return userId;
}

public void setUserId(int userId) {
    this.userId = userId;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getBody() {
    return body;
}

public void setBody(String body) {
    this.body = body;
}


@Override
public String toString() {
    return "PostModel{" +
            "userId=" + userId +
            ", id=" + id +
            ", title='" + title + '\'' +
            ", body='" + body + '\'' +
            '}';
}
}

I really don’t understand what is the reason.As I understand it, reading in the documentation, it should read the file and present it in the java class. Any sugestions?

>Solution :

There is no method signature supposed to get a file path as first argument. You may pass a JSON String as first argument or you could use the method signature with a File Object as first argument, like this:

public static PostPojo[] readFile(String titleFile){
    String pathJSONFile = "src/main/resources/"+titleFile+".json";
    ObjectMapper objectMapper = new ObjectMapper();
    File jsonFile = new File(pathJSONFile);

    PostPojo[] postPojo = null;
    try {
        postPojo = objectMapper.readValue(jsonFile, PostPojo[].class);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return postPojo;
}

EDIT: Since your file defines a wrapping array around the object you have to parse it as array. Afterwards you may return it as an array like i did in my edited answer or you just return the first array record.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading