0

I have a json object that has nested objects, like this:

{
  "process": "create",
  "processDescription": "some description",
  "book": {
    "id": "some unique id",
    "bookName": "some name",
    "bookISBN": "some number",
    "bookDesc": "some description",    
    "author": {
      "id": "a different ID",
      "firstName": "some name",
      "lastName": "some description"
    },
    "timestampUpdated": null,
    "timestampCreated": 1672753599223,
    "createdByUser": "Jane Doe",
    "updatedByUser": null
  },
  "someField": "some data",
  "anotherField": "more data"
}

In this example, I am only concerned with the "book" object and the "author" object. So, I have Java Classes for both of them. I've tried to read in the json string like this:

JsonNode jsonObject = new ObjectMapper().readTree(jsonString);
JsonNode bookObj = jsonObject.get("book");
JsonNode authorObj = bookObj.get("author");

ObjectMapper objectMapper = new ObjectMapper();
AuthorClass = objectMapper.readValue(authorObj.asText(), AuthorClass .class);

but this causes an error:

com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input at [Source: (String)""; line: 1, column: 0]

2 Answers 2

2

To deserialize just a nested part of a json file you can convert your json file to a JsonNode object and use the JsonNode#at method:

//jsonnode is equal to
//{"id":"a different ID","firstName":"some name","lastName":"some description"}
JsonNode authornode = mapper.readTree(json).at("/book/author");      

Then you can convert the JsonNode object to a AuthorClass object with the treeToValue method:

@Data
public class AuthorClass {
    private String id;
    private String firstName;
    private String lastName;
}

JsonNode authornode = mapper.readTree(json).at("/book/author");
AuthorClass author = mapper.treeToValue(authornode, AuthorClass.class);
//it prints
//AuthorClass(id=a different ID, firstName=some name, lastName=some description)
System.out.print(author);
Sign up to request clarification or add additional context in comments.

Comments

1

You should use method authorObj.toString() instead of method authorObj.asText()

Working example:

 public class Example {

    public static void main(String[] args) throws JsonProcessingException {
        String input = "{\n" +
            "  \"process\": \"create\",\n" +
            "  \"processDescription\": \"some description\",\n" +
            "  \"book\": {\n" +
            "    \"id\": \"some unique id\",\n" +
            "    \"bookName\": \"some name\",\n" +
            "    \"bookISBN\": \"some number\",\n" +
            "    \"bookDesc\": \"some description\",    \n" +
            "    \"author\": {\n" +
            "      \"id\": \"a different ID\",\n" +
            "      \"firstName\": \"some name\",\n" +
            "      \"lastName\": \"some description\"\n" +
            "    },\n" +
            "    \"timestampUpdated\": null,\n" +
            "    \"timestampCreated\": 1672753599223,\n" +
            "    \"createdByUser\": \"Jane Doe\",\n" +
            "    \"updatedByUser\": null\n" +
            "  },\n" +
            "  \"someField\": \"some data\",\n" +
            "  \"anotherField\": \"more data\"\n" +
            "}";


        JsonNode jsonObject = new ObjectMapper().readTree(input);
        JsonNode bookObj = jsonObject.get("book");
        JsonNode authorObj = bookObj.get("author");

        ObjectMapper objectMapper = new ObjectMapper();
        AuthorClass authorClass = objectMapper.readValue(authorObj.toString(), AuthorClass.class);
        System.out.println(authorClass.getFirstName());
    }
}

class AuthorClass {
    private String firstName;
    private String lastName;
    private String id;

    public AuthorClass() {
    }

    public AuthorClass(String firstName, String lastName, String id) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.id = id;
    }

    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 getId() {
        return id;
    }

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

Comments

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.