In order to convert a Java to JSON object using Jackon, we first need to add the Jackson library to the project dependencies,
Adding Jackson dependency to Java Gradle Project
Jackson Gradle Dependency:
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.4'
Jackson Maven Dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4</version>
</dependency>
Java Code Example using Jackson ObjectMapper:

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
Album album = new Album();
album.setArtistName("Michael Jackson");
album.setAlbumName("Thriller");
String[] songs = new String[9];
songs[0] = "Wanna Be Startin Somethin";
songs[1] = "Baby Be Mine";
songs[2] = "The Girl Is Mine";
songs[3] = "Thriller";
songs[4] = "Beat it";
songs[5] = "Billie Jean";
songs[6] = "Human Nature";
songs[7] = "P.Y.T. (Pretty Young Thing)";
songs[8] = "The Lady in My Life";
album.setSongsList(songs);
album.setReleaseYear(1982);
try {
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(album);
System.out.println(jsonString);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Album.java (POJO)
public class Album {
private String artistName;
private String albumName;
private int releaseYear;
private String[] songsList;
public String getArtistName() {
return artistName;
}
public void setArtistName(String artistName) {
this.artistName = artistName;
}
public String getAlbumName() {
return albumName;
}
public void setAlbumName(String albumName) {
this.albumName = albumName;
}
public int getReleaseYear() {
return releaseYear;
}
public void setReleaseYear(int releaseYear) {
this.releaseYear = releaseYear;
}
public String[] getSongsList() {
return songsList;
}
public void setSongsList(String[] songsList) {
this.songsList = songsList;
}
}
Json Output:
{
"artistName":"Michael Jackson",
"albumName":"Thriller",
"releaseYear":1982,
"songsList":[
"Wanna Be Startin Somethin",
"Baby Be Mine",
"The Girl Is Mine",
"Thriller",
"Beat it",
"Billie Jean",
"Human Nature",
"P.Y.T. (Pretty Young Thing)",
"The Lady in My Life"
]
}
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Jupyter Notebook: 500 Internal Server Error - nbconvert failed: xelatex not found on PATH - Python
- Convert String Date to Date Object in Java - Java
- How to turn off Stage Manager - macOS Ventura - MacOS
- Read a file line by line in Python Program - Python
- JDBCTemplate Querying Examples with Spring Boot 3 - Java
- Display ls command file sizes in KB (kilobytes) MB (megabytes) or GB (gigabytes) [Linux/macOS] - MacOS
- Change Title text for Android Activity using java code - Android
- Java JDBC Connection with Database using SSL (https) URL - Java