obsessiveCookie
obsessiveCookie

Reputation: 1138

Removing a node in json in Java

Given the following json:

{
   "User":{
      "firstname":"john",
      "gender":"female",
      "verified":"no"
   }
}

Is there a way to remove/ignore "User" node so I can use the jackson bind ? You see, at the moment if I try and do this:

User user= mapper.readValue(content, User.class); //where content is the json above

where User class is:

public class User
{
     String _firstname, _gender, _verified;

     [getters and setters]
}

it gives an error saying:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "User"

I know I can just initialise the User class and just do setters manually but I was wondering if it is possible or better to remove/ignore the "User" node instead?

Upvotes: 1

Views: 4133

Answers (2)

Kevin Bowersox
Kevin Bowersox

Reputation: 94479

This solution uses pure Jackson by setting the rootName on the ObjectReader:

import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;

public class User {

    public static void main(String[] args) throws JsonProcessingException, IOException {

        String json = "{\"User\":{\"firstname\":\"john\",\"gender\":\"female\",\"verified\":\"no\"}}";
        ObjectMapper mapper = new ObjectMapper();
        ObjectReader reader = mapper.reader(User.class).withRootName("User");
        User user = reader.readValue(json);

        System.out.println(user.getFirstname());
    }

    private String firstname;
    private String lastname;
    private String verified;
    private String gender;

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getVerified() {
        return verified;
    }

    public void setVerified(String verified) {
        this.verified = verified;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

}

Upvotes: 2

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280031

In the root of your JSON, you have another JSON object named User. You can't just ignore it since it contains the object you are trying to extract. What you need to do is extract the object from that nested JSON object instead of the root JSON object.

You can do that by getting the nested JSON object.

ObjectMapper mapper = new ObjectMapper(); 
JsonNode node = mapper.readTree(content);
User user = mapper.readValue(node.get("User").traverse(), User.class);

Also, I'm not sure if Jackson supports dangling , as you have at the end of

"verified":"no",

Upvotes: 3

Related Questions