📘 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
- JsonParser - It is the base class of Jackson API for reading JSON content.
- JsonGenerator - It is the base class of Jackson API for writing JSON content.
Dependencies
<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
Using JsonGenerator for Writing JSON content
JsonFactory factory = new JsonFactory();
JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createGenerator(
new File("post.json"), JsonEncoding.UTF8);
package net.javaguides.jackson;
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
/**
* Write JSON from file using JsonGenerator
* @author Ramesh Fadatare
*
*/
public class WriteJsonUsingJsonGenerator {
public static void main(String[] args) throws IOException {
JsonFactory factory = new JsonFactory();
// Create JsonGenerator
JsonGenerator generator = factory.createGenerator(new File("post.json"), JsonEncoding.UTF8);
// Creating JSON Oject
generator.writeStartObject(); // Start with left brace i.e. {
// Add string field
generator.writeNumberField("id", 100);
generator.writeStringField("title", "Jackson JSON API Guide");
generator.writeStringField("description", "Post about Jackson JSON API");
generator.writeStringField("content", "HTML content here");
// Creating JSON Array within JSON Object
generator.writeFieldName("tags");
// Array of Tag objects
generator.writeStartArray(); // Start with left bracket i.e. [
// first tag object
generator.writeStartObject(); // Start with left brace i.e. {
generator.writeNumberField("id", 1);
generator.writeStringField("name", "JSON");
generator.writeEndObject(); // End with right brace i.e }
// second tag object
generator.writeStartObject(); // Start with left brace i.e. {
generator.writeNumberField("id", 2);
generator.writeStringField("name", "Java");
generator.writeEndObject(); // End with right brace i.e }
// second tag object
generator.writeStartObject(); // Start with left brace i.e. {
generator.writeNumberField("id", 3);
generator.writeStringField("name", "Jackson");
generator.writeEndObject(); // End with right brace i.e }
generator.writeEndArray(); // End with right bracket i.e. ]
generator.writeEndObject(); // End with right brace i.e }
// Close JSON generator
generator.close();
// Print JSON Object
}
}
post.json file
{
"id" : 100,
"title" : "Jackson JSON API Guide",
"description" : "Post about Jackson JSON API",
"content" : "HTML content here",
"tags" : [ {
"id" : 1,
"name" : "JSON"
}, {
"id" : 2,
"name" : "Java"
}, {
"id" : 3,
"name" : "Jackson"
} ]
}
Using JsonParser for Reading JSON Content
package net.javaguides.jackson;
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
/**
* Read JSON from file using JsonParser
* @author Ramesh Fadatare
*
*/
public class ReadJsonUsingJsonParser {
public static void main(String[] args) throws IOException {
JsonFactory factory = new JsonFactory();
// Create Reader/InputStream/File
File file = new File("post.json");
// Create JsonParser
JsonParser parser = factory.createParser(file);
JsonToken token = parser.nextToken(); // Read first object i.e. {
// Read JSON object
token = parser.nextToken();
if (token == JsonToken.FIELD_NAME && "id".equals(parser.getCurrentName())) {
token = parser.nextToken();
if (token == JsonToken.VALUE_STRING) {
System.out.println("ID : " + parser.getText());
}
}
token = parser.nextToken();
if (token == JsonToken.FIELD_NAME && "title".equals(parser.getCurrentName())) {
token = parser.nextToken();
if (token == JsonToken.VALUE_STRING) {
System.out.println("title : " + parser.getText());
}
}
token = parser.nextToken();
if (token == JsonToken.FIELD_NAME && "description".equals(parser.getCurrentName())) {
token = parser.nextToken();
if (token == JsonToken.VALUE_STRING) {
System.out.println("description : " + parser.getText());
}
}
token = parser.nextToken();
if (token == JsonToken.FIELD_NAME && "content".equals(parser.getCurrentName())) {
token = parser.nextToken();
if (token == JsonToken.VALUE_STRING) {
System.out.println("content : " + parser.getText());
}
}
// Reading JSON Array
token = parser.nextToken();
if (token == JsonToken.FIELD_NAME && "tags".equals(parser.getCurrentName())) {
System.out.println("Post tags are - ");
token = parser.nextToken(); // // Read left bracket i.e. [
// Loop to print array elements until right bracket i.e ]
while (token != JsonToken.END_ARRAY) {
token = parser.nextToken();
if (token == JsonToken.VALUE_STRING) {
System.out.println(parser.getText());
}
}
System.out.println();
}
parser.close();
}
}
title : Jackson JSON API Guide
description : Post about Jackson JSON API
content : HTML content here
Post tags are -
JSON
Java
Jackson
Related Articles
- Change Field Name in JSON using Jackson (popular)
Comments
Post a Comment
Leave Comment