Convert Java String to JSON Object using Jackson

Let us see how to convert a Java String to JSON Object using Jackson Library,

Example - Java String to JSON Object

Java String
String javaJsonString = "{\"name\":\"Chris\",\"age\":22}";

Java Code:
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;


public class JavaStringToJson {

    public static void main(String[] args) {
        try {
            String javaJsonString = "{\"name\":\"Chris\",\"age\":22}";
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(javaJsonString);
            String name = jsonNode.get("name").asText();
            int age = jsonNode.get("age").asInt();
            System.out.println(name +" : " + age);

        } catch (IOException e) {
        } catch (Exception e) {

Chris : 22

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap