Convert Object to JSON and JSON to Object with FasterXML/jackson-databind

Refer the FasterXml/Jackson-bind API from the below GitHub repo. This is very helpful for handling the various data format especially JSON

The sample code to convert Object to JSON and JSON to Object is given below,

Maven Dependencies,



import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class JSONUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtils.class);

    public static void main(String[] args) throws IOException {"Converting Object to String");
        Customer customer = new Customer();
        String objectString = convertObjectAsString(customer);"Converting String:{}", objectString);"Converting String to Customer Object");
        Customer customer1 = convertJsonStringAsObject(objectString);"Converted Object:{}", customer1);


    public static String convertObjectAsString(Object obj) throws JsonProcessingException {
        String content = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            content = mapper.writeValueAsString(obj);
        } catch (Exception e) {
            LOGGER.error("Exception happened while doing the conversion", e);
            throw e;
        return content;

    public static Customer convertJsonStringAsObject(String content) throws IOException {

        Customer customer = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
            customer = mapper.readValue(content, Customer.class);
        } catch (Exception e) {
            LOGGER.error("Exception happened while doing the conversion", e);
            throw e;
        return customer;

    public static class Customer {

        private String firstName;
        private String lastName;
        private String customerId;

        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 getCustomerId() {
            return customerId;

        public void setCustomerId(String customerId) {
            this.customerId = customerId;


        public boolean equals(Object o) {
            if (this == o) return true;

            if (o == null || getClass() != o.getClass()) return false;

            Customer customer = (Customer) o;

            return new EqualsBuilder()
                    .append(firstName, customer.firstName)
                    .append(lastName, customer.lastName)
                    .append(customerId, customer.customerId)

        public int hashCode() {
            return new HashCodeBuilder(17, 37)

        public String toString() {
            return new ToStringBuilder(this)
                    .append("firstName", firstName)
                    .append("lastName", lastName)
                    .append("customerId", customerId)



Converting Object to String
Converting String:{"firstName":"Bala","lastName":"samy","customerId":"cust1234"}
Converting String to Customer Object
Converted Object:JSONUtils$Customer@130f889[firstName=Bala,lastName=samy,customerId=cust1234]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s