Jackson to convert Java Object to/from Json String

The tutorial will guide you how to use Jackson for converting Java object to Json and vice-versa.

Related Articles:
Gson for converting Java Object to/from Json
How to convert Java Object into/from XML – with Castor & Spring Boot
How to use @JsonView to serialize/de-serialize and customize JSON format from Java Object.

A. Introduce

Jackson is common library beside GSON that is used for serialize or map Java objects to JSON and vice versa.
The tutorial uses 2 methods for converting:
writeValueAsString(): Convert Java object to String as Json format
readValue(): Convert String with JSON format to Java object

B. Practice

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE (It’s OK if you use Eclipse)

II. Overview

1. Project Structure

jackson project structure

2. Step to do

– Create Maven Project with Spring Toot Suite
– Add Jackson dependency
– Create Java model class
– Use Jackson functions for converting Java Object to Json and vice-versa
– Run & Check result

III. Practice

1. Create Maven Project with Spring Toot Suite

Open Spring Tool Suite, on main menu, choose File->New->Maven Project, check Create a simple project (skip archetype selection), press Next. Then input project info:

input json-jackson project info

Press Finish, Maven project is created successfully.

2. Add Jackson dependency

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javasampleapproach</groupId>
	<artifactId>json-jackson</artifactId>
	<version>0.0.1</version>
	<name>jackson</name>
	<description>Using Jackson library for converting JavaObject To Json and vice-versa</description>

	<dependencies>
		<!-- http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.8.5</version>
		</dependency>
	</dependencies>
</project>

3. Create Java model class

– Create a Customer model class:


package com.javasampleapproach.jackson.model;

public class Customer {
	private long id;
	private String firstname;
	private String lastname;
	
	public Customer(long id, String firstname, String lastname){
		this.id = id;
		this.firstname = firstname;
		this.lastname = lastname;
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		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;
	}
	
	@Override
	public String toString() {
		String info = String.format("Customer Info: id = %d, firstname = %s, lastname = %s", id, firstname, lastname);
		return info;
	}
}

4. Use Jackson functions for converting Java Object to Json and vice-versa

– Convert Java Object to String as Json format, use writeValueAsString:


ObjectMapper mapper = new ObjectMapper();
try {
	
	Customer customer = new Customer(1, "Jack", "Kennedy");
	jsonString = mapper.writeValueAsString(customer);
	System.out.println("jsonString = " + jsonString);
} catch (JsonProcessingException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

– Convert String as Json Format to Java Object, use: readValue


try {
	Customer convertedCustomer = mapper.readValue(jsonString, Customer.class);
	System.out.println("convertedCustomer info: " + convertedCustomer.toString());
} catch (JsonParseException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (JsonMappingException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

5. Run & Check result

– Full Application class:


package com.javasampleapproach.jackson;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.javasampleapproach.jackson.model.Customer;

public class Application {
	public static void main(String[] args) {

		String jsonString = "";

		//
		// Convert Java Object to JSON String
		//
		ObjectMapper mapper = new ObjectMapper();
		try {

			Customer customer = new Customer(1, "Jack", "Kennedy");
			jsonString = mapper.writeValueAsString(customer);
			System.out.println("jsonString = " + jsonString);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//
		// Convert String as Json Format to Java Object
		//
		try {
			Customer convertedCustomer = mapper.readValue(jsonString, Customer.class);
			System.out.println("convertedCustomer info: " + convertedCustomer.toString());
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

– Run & Result:


jsonString = {"id":1,"firstname":"Jack","lastname":"Kennedy"}
convertedCustomer info: Customer Info: id = 1, firstname = Jack, lastname = Kennedy

IV. Source code

json-jackson sourcecode

2 thoughts on “Jackson to convert Java Object to/from Json String”

  1. I really love your blog.. Great colors & theme. Did you create this site yourself?
    Please reply back as I’m planning to create my very
    own site and would like to find out where you got
    this from or exactly what the theme is called. Thank you!

  2. 72782 723442Cause thats required valuable affiliate business rules to get you started on participating in circumstances appropriate for your incredible web-based business concern. Inernet marketing 704399

Leave a Reply

Your email address will not be published. Required fields are marked *