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

How to convert a java object into a Json

I have a code where I need to compare an email(string) coming from the frontend to the ones storaged in my ddbb. The thing is that the ones storaged are email objects with differents fields as verified and createdBy let say.
So what I need to check is only the email of this object is equal to the upcoming email from the frontend.

This will be the object:

"addressInformation": {
        "email": "test@it-xpress.eu",
        "verified": true,
        "versource": "n70007"
    }

and then I want to compare it with a string email = "test@it-xpress.eu"
Maybe Json.stringfy?

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

Cheers!

>Solution :

You have a two methods:

Read to JsonNode:

new ObjectMapper().readTree("{\"email\": \"test@it-xpress.eu\"}").get("email").asText()

new ObjectMapper().valueToTree(myObject).get("email").asText()

Read to specific object:

class MyObject {
   private String email;
   public String getEmail() { return email; }
}
new ObjectMapper().readValue(MyObject.class).getEmail()

ATTENTION!
If you use Spring, Play, Guice or another dependency framework please use inject existing ObjectMapper or ObjectMapperBuilder

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