I introduced you to the JSON Binding specification of Jakarta EE to convert between JSON data and Java objects. To parse, generate, transform, or query JSON data, you can use another Jakarta EE specification, JSON Processing. How is it in details? We will learn together about JSON Processing and its implementation Eclipse Parsson in this tutorial!
First, I will create a new Maven project as an example:
Eclipse Parsson dependency is as follows:
1 2 3 4 5 |
<dependency> <groupId>org.eclipse.parsson</groupId> <artifactId>parsson</artifactId> <version>1.1.1</version> </dependency> |
Similar to JSON Binding, you can generate JSON data from a Java object using JSON Processing.
For example, I have a Student class that defines student information as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.huongdanjava.jakartaee; public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } } |
You can generate JSON data of student information using the write() method of the JsonWriter class, which is initialized from the Json class with the createWriter() method. But to do this, you need to define JSON data with the JsonObject class using the JsonObjectBuilder class first:
1 2 3 4 5 6 7 |
Student student = new Student("Khanh", 35); JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder() .add("name", student.getName()) .add("age", student.getAge()); JsonObject jsonObject = jsonObjectBuilder.build(); |
Now we will use an implementation of the Writer class like StringWriter to write this student information to JSON data as follows:
1 2 3 4 5 6 7 |
String json; try (Writer writer = new StringWriter()) { Json.createWriter(writer).write(jsonObject); json = writer.toString(); } System.out.println(json); |
Result:
To format the generated JSON string, you can use the JsonWriterFactory class.
This JsonWriterFactory class allows us to configure how we generate the JSON string. We will need a Map object containing configuration information with key and value, then create a new JsonWriterFactory object with this configuration information.
For example, to format the generated JSON string, I will initialize the Map object containing the configuration information first:
1 2 |
Map<String, Boolean> config = new HashMap<>(); config.put(JsonGenerator.PRETTY_PRINTING, true); |
Then initialize the JsonWriterFactory object:
1 |
JsonWriterFactory writerFactory = Json.createWriterFactory(config); |
Now we will use the createWriter() method of the JsonWriterFactory object instead of the object of the Json class to create a new Writer and write the student information out:
1 2 3 4 5 6 7 |
String json; try (Writer writer = new StringWriter()) { writerFactory.createWriter(writer).write(jsonObject); json = writer.toString(); } System.out.println(json); |
Results when running the example again:
JSON Processing also allows us to convert a JSON data string to a Java object, but the way it works is different from JSON Binding.
We will use the JsonReader object to read the content of JSON data and put it into the JsonObject object.
1 2 3 4 5 6 7 8 9 |
String json = """ { "name": "Khanh", "age": 35 } """; JsonReader reader = Json.createReader(new StringReader(json)); JsonObject jsonObject = reader.readObject(); |
Then initialize the Student object, and get information about the properties of this Student object from the above JsonObject object as follows:
1 2 3 4 5 |
Student student = new Student(); student.setName(jsonObject.getString("name")); student.setAge(jsonObject.getInt("age")); System.out.println(student); |
Result:
I will show you how to query and transform JSON data using JSON Processing in the following tutorials!