Just putting the comment by @pvpkiran in an answer.

Use ObjectMapper class from com.fasterxml.jackson.databind

ObjectMapper objectMapper = new ObjectMapper();

Converting from Object to String:

String jsonString = objectMapper.writeValueAsString(link);

Converting from String to Object:

Link link = objectMapper.readValue(jsonString, type)
Answer from ytibrewala on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ jackson โ€บ intro to the jackson objectmapper
Intro to the Jackson ObjectMapper | Baeldung
December 9, 2025 - This tutorial focuses on understanding the Jackson ObjectMapper class and how to serialize Java objects into JSON and deserialize JSON string into Java objects.
Discussions

JSON string to Java object with Jackson - Stack Overflow
I am quite fascinated by the ObjectMapper's readValue(file, class) method, found within the Jackson library which reads a JSON string from a file and assigns it to an object. More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - ObjectMapper convert string value(json format) to object - Stack Overflow
I want to deserialize hobby_list string value as object. More on stackoverflow.com
๐ŸŒ stackoverflow.com
July 27, 2021
java - Jackson Library JSON Mapper to String - Stack Overflow
I have a class (A and B are my objects) public A { private List b; private int c; } I have a String variable temp. So i would save in this variable the JSON String of my object.... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - ObjectMapper - Parse String to Object - Stack Overflow
I try to parse my String to Object using ObjectMapper.readValue(...) I have String line = {"jobID":"123","clientID":"555","userID":"444"}... More on stackoverflow.com
๐ŸŒ stackoverflow.com
July 5, 2021
๐ŸŒ
Medium
medium.com โ€บ @ujjawalr โ€บ convert-json-to-object-and-vice-versa-with-jackson-objectmapper-java-be9c47ab5954
Convert JSON To Object And Vice-Versa With Jackson ObjectMapper Java | by Ujjawal Rohra | Medium
August 17, 2024 - This article is a guide to ObjectMapper class from Jackson library and how it is used to convert a json string to java object and a java object to a json string.
๐ŸŒ
Jenkov
jenkov.com โ€บ tutorials โ€บ java-json โ€บ jackson-objectmapper.html
Jackson ObjectMapper
You can convert a JsonNode to a Java object, using the Jackson ObjectMapper treeToValue() method. This is similar to parsing a JSON string (or other source) into a Java object with the Jackson ObjectMapper.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ objectmapper
ObjectMapper in Java: Conversion with the Jackson API
March 5, 2024 - Deserialization is the reverse process: converting a JSON string back into a Java object. ObjectMapper plays a crucial role in both these processes. It provides methods like writeValueAsString for serialization and readValue for deserialization, ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-convert-a-json-string-to-a-map-using-jackson
How to convert a JSON string to a map using Jackson
Use the method readValue() of the ObjectMapper class, which takes in a JSON string and the class the string will be converted to.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @salvipriya97 โ€บ objectmapper-and-its-methods-examples-in-java-4a4cab75cb6b
ObjectMapper and its methods examples in Java | by Priya Salvi | Medium
June 25, 2024 - Deserialization: Converting JSON strings to Java objects. Tree Model: Creating and manipulating JSON tree structures. Data Binding: Mapping JSON data directly to Java objects, making it easy to handle complex data structures.
๐ŸŒ
amitph
amitph.com โ€บ home โ€บ java โ€บ read json strings into java objects with jackson api
Read JSON Strings into Java Objects with Jackson API - amitph
November 22, 2024 - To read JSON String and Map into Java Object, we can use the readValue method. We need to provide the JSON string and a target class. private static void jsonToObject() throws JsonProcessingException { String json = """ { "id":1111, ...
๐ŸŒ
DevQA
devqa.io โ€บ map-json-string-to-java-object
How to Map a JSON String to a Java Object
July 11, 2023 - To map a JSON string to a Java object using Jackson, follow these steps: Include the necessary Jackson dependencies in your projectโ€™s build file. For Maven, add the following dependencies to your pom.xml file: <dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.5</version> </dependency> </dependencies> import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonExample { public static void main(String[] args) throws Exception { String jsonString = "{\"name\":\"John Doe\",\"age\":25,\"email\":\"johndoe@example.com\"}"; ObjectMapper objectMapper = new ObjectMapper(); Person person = objectMapper.readValue(jsonString, Person.class); System.out.println(person.getName()); System.out.println(person.getAge()); System.out.println(person.getEmail()); } }
๐ŸŒ
GitHub
github.com โ€บ tristanhimmelman โ€บ ObjectMapper
GitHub - tristanhimmelman/ObjectMapper: Simple JSON Object mapping written in Swift
... To support mapping, a class or struct just needs to implement the Mappable protocol which includes the following functions: ... ObjectMapper uses the <- operator to define how each member variable maps to and from JSON. class User: Mappable ...
Starred by 9.2K users
Forked by 1K users
Languages ย  Swift 99.2% | Swift 99.2%
Top answer
1 of 1
16

The code should pass an instance of Type A to the writeValueAsString method not the actual class.

The ObjectMapper will be able to determine the instance's type via reflection, however if you don't pass an instance it cannot determine the field value's to place in the generated JSON.

Also be sure to catch or throw the appropriate exceptions.

public void save() {

try {
        A a = new A();
        ObjectMapper mapper = new ObjectMapper();
        String temp = mapper.writeValueAsString(a);

    } catch (JsonGenerationException e) {
       e.printStackTrace();
    } catch (JsonMappingException e) {
       e.printStackTrace();
    } catch (IOException e) {
       e.printStackTrace();
    }

}

You should also have acessors for the fields on class A and class B. NOTICE I included B in the previous sentence, since Jackson must be able to map all fields on the instance we provide to it.

public A   {
    private List<B> b;
    private int c;

    private B getB(){ return b;}
    private void setB(B b){this.b = b;}
    private int getC(){return c;}
    private void setC(int c){this.c = c;}
}

Here is a complete working example that I have tested.

import java.io.IOException;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class A {

    private List<B> b;
    private int c;

    public List<B> getB() {
        return b;
    }

    public void setB(List<B> b) {
        this.b = b;
    }

    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }

    public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
        A a = new A();
        ObjectMapper mapper = new ObjectMapper();
        String temp = mapper.writeValueAsString(a);
        System.out.println(temp);   
    }
}

class B{


}

If this simple example does not work the jackson-mapper-asl.jar file is most likely not on the build path. Download the file from here and place it somewhere on your local machine.

Next via Eclipse, Right Click on your Project and select properties.

Then select Build Path > Libraries > Add External Jars. Locate the jar and then hit OK.

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 68256220 โ€บ objectmapper-parse-string-to-object
java - ObjectMapper - Parse String to Object - Stack Overflow
July 5, 2021 - I try to parse my String to Object using ObjectMapper.readValue(...) I have String line = {"jobID":"123","clientID":"555","userID":"444"}...
๐ŸŒ
Javathinking
javathinking.com โ€บ blog โ€บ convert-string-to-object-java-objectmapper
Convert String to Object in Java using ObjectMapper | JavaThinking.com
July 19, 2025 - The ObjectMapper class is part of the Jackson library. It provides methods to read JSON content and convert it into Java objects (readValue method) and to write Java objects as JSON strings (writeValueAsString method).
๐ŸŒ
Fasterxml
fasterxml.github.io โ€บ jackson-databind โ€บ javadoc โ€บ 2.7 โ€บ com โ€บ fasterxml โ€บ jackson โ€บ databind โ€บ ObjectMapper.html
ObjectMapper (jackson-databind 2.7.0 API)
ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionality for performing conversions. It is also highly customizable to work both with different styles of JSON content, and to support more advanced Object concepts such as polymorphism and Object identity.
๐ŸŒ
Medium
medium.com โ€บ @salvipriya97 โ€บ java-objectmapper-explained-c33cdc4876d1
Java ObjectMapper explained. In Java, an object mapper is a toolโ€ฆ | by Priya Salvi | Medium
April 21, 2024 - In this example, we use the readValue() method of ObjectMapper to convert the JSON string into a Person object.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ jackson โ€บ jackson_objectmapper.htm
Jackson - ObjectMapper Class
ObjectMapper is the main actor class of Jackson library. ObjectMapper class ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ definitive-guide-to-jackson-objectmapper-serialize-and-deserialize-java-objects
Definitive Guide to Jackson ObjectMapper - Serialize and Deserialize Java Objects
October 27, 2023 - This is typically done when you receive a response containing a JSON-serialized entity, and would like to convert it to an object for further use. With ObjectMapper, to convert a JSON string into a Java Object, we use the readValue() method.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 26055261
java - use ObjectMapper parse string json to object - Stack Overflow
to region or coordinate object,but failed, i use objectMapper.readValue (str, Coordinate[].class); and objectMapper.readValue (str, Region.class); all not complete.