📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
POM Dependency
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
- jackson-annotations-2.9.8.jar
- jackson-core-2.9.8.jar
- jackson-databind-2.9.8.jar
Converting Java Object to JSON Example
Post.java
package net.javaguides.jackson.pojotojson;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
public class Post {
private Long id;
private String title;
private String description;
private String content;
private Date postedAt = new Date();
private Date lastUpdatedAt = new Date();
private Set < Tag > tags = new HashSet < > ();
public Post() {
}
public Post(String title, String description, String content) {
this.title = title;
this.description = description;
this.content = content;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getPostedAt() {
return postedAt;
}
public void setPostedAt(Date postedAt) {
this.postedAt = postedAt;
}
public Date getLastUpdatedAt() {
return lastUpdatedAt;
}
public void setLastUpdatedAt(Date lastUpdatedAt) {
this.lastUpdatedAt = lastUpdatedAt;
}
public Set < Tag > getTags() {
return tags;
}
public void setTags(Set < Tag > tags) {
this.tags = tags;
}
}
Tag.java
package net.javaguides.jackson.pojotojson;
public class Tag {
private Long id;
private String name;
public Tag() {
}
public Tag(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Tag(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
JacksonPojoToJson.java
package net.javaguides.jackson.pojotojson;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
* Convert Java to Json using Jackson API
* @author Ramesh Fadatare
*
*/
public class JacksonPojoToJson {
public static void main(String[] args) throws IOException {
// Create ObjectMapper
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
// create a post
Post post = new Post();
post.setTitle("Jackson JSON API Guide");
post.setId(100 L);
post.setDescription("Post about Jackson JSON API");
post.setContent("HTML content here");
post.setLastUpdatedAt(new Date());
post.setPostedAt(new Date());
// create some predefined tags
Set < Tag > tags = new HashSet < > ();
Tag java = new Tag(1 L, "Java");
Tag jackson = new Tag(2 L, "Jackson");
Tag json = new Tag(3 L, "JSON");
tags.add(java);
tags.add(jackson);
tags.add(json);
// set tags to post
post.setTags(tags);
// Convert object to JSON string
String postJson = mapper.writeValueAsString(post);
System.out.println(postJson);
// Save JSON string to file
FileOutputStream fileOutputStream = new FileOutputStream("post.json");
mapper.writeValue(fileOutputStream, post);
fileOutputStream.close();
}
}
{
"id" : 100,
"title" : "Jackson JSON API Guide",
"description" : "Post about Jackson JSON API",
"content" : "HTML content here",
"postedAt" : 1556025668077,
"lastUpdatedAt" : 1556025668077,
"tags" : [ {
"id" : 3,
"name" : "JSON"
}, {
"id" : 1,
"name" : "Java"
}, {
"id" : 2,
"name" : "Jackson"
} ]
}
Comments
Post a Comment
Leave Comment