Home > database >  Spring Boot parse nested json into separate objects
Spring Boot parse nested json into separate objects

Time:01-17

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]

CodePudding user response:

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);

CodePudding user response:

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;
    }
}
  • Related