How to execute nested group mail merge with JSON in Word document using Java?
Mail merge is a process of merging data from a data source into a Word template document. Syncfusion®Java Word library (Essential® DocIO) is used to generate reports like invoices, payrolls, letters, and more by performing a mail merge faster in a batch process without Microsoft Word or interop dependencies. Using this library, you can execute nested group mail merge with JSON in a Word document using Java.
Execute nested group mail merge with JSON in Word document using Java:
- Create a new Java file and include the following namespaces.
JAVA
import com.syncfusion.docio.*;
import com.syncfusion.javahelper.system.collections.generic.DictionarySupport;
import com.syncfusion.javahelper.system.collections.generic.ListSupport;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
- Use the following code sample to perform nested mail merge using a JSON string in a Word document.
JAVA
// JSON string containing nested organizational data
String json = "{\"Organizations\":[{\"BranchName\":\"UK Office\",\"Address\":\"120 Hanover Sq.\",\"City\":\"London\",\"ZipCode\":\"WX1 6LT\",\"Country\":\"UK\",\"Departments\":[{\"DepartmentName\":\"Marketing\",\"Supervisor\":\"Nancy Davolio\",\"Employees\":[{\"EmployeeName\":\"Thomas Hardy\",\"EmployeeID\":\"1001\",\"JoinedDate\":\"05/27/1996\"},{\"EmployeeName\":\"Maria Anders\",\"EmployeeID\":\"1002\",\"JoinedDate\":\"04/10/1998\"}]},{\"DepartmentName\":\"Production\",\"Supervisor\":\"Andrew Fuller\",\"Employees\":[{\"EmployeeName\":\"Elizabeth Lincoln\",\"EmployeeID\":\"1003\",\"JoinedDate\":\"05/15/1996\"},{\"EmployeeName\":\"Antonio Moreno\",\"EmployeeID\":\"1004\",\"JoinedDate\":\"04/22/1996\"}]}]}]}";
// Convert JSON string into a JsonObject
JsonObject data = new Gson().fromJson(json, JsonObject.class);
// Convert JsonObject to DictionarySupport (required format for mail merge)
DictionarySupport<String, Object> result = getData(data);
// Load the Word document template
WordDocument document = new WordDocument("Template.docx");
// Extract the list of organizations for mail merge
MailMergeDataTable dataTable = new MailMergeDataTable("Organizations", (ListSupport<Object>) result.get("Organizations"));
// Perform nested mail merge with the data table
document.getMailMerge().executeNestedGroup(dataTable);
// Save the generated Word document
document.save("Result.docx", FormatType.Docx);
document.close();
System.out.println("Word document generated successfully.");
- Use the following helper method to convert a JsonObject into DictionarySupport.
JAVA
public static DictionarySupport<String, Object> getData(JsonObject data) throws Exception {
DictionarySupport<String, Object> map = new DictionarySupport<>(String.class, Object.class);
for (String key : data.keySet()) {
Object keyValue = null;
if (data.get(key).isJsonArray()) {
// Convert array to ListSupport
keyValue = getData(data.getAsJsonArray(key));
} else if (data.get(key).isJsonPrimitive()) {
// Use primitive value directly
keyValue = data.get(key).getAsString();
}
map.add(key, keyValue);
}
return map;
}
- Use the following helper method to convert a JsonArray into ListSupport.
JAVA
// Converts a JsonArray into ListSupport recursively
public static ListSupport<Object> getData(JsonArray arr) throws Exception {
ListSupport<Object> list = new ListSupport<>(Object.class);
for (int i = 0; i < arr.size(); i++) {
Object keyValue = null;
if (arr.get(i).isJsonObject()) {
// Recursively convert nested objects
keyValue = getData(arr.get(i).getAsJsonObject());
}
list.add(keyValue);
}
return list;
}
You can download a complete working sample to execute nested group mail merge with JSON in a Word document from GitHub.
By executing the application, you will get the output Word document as follows.
You can refer to this link to learn how to run the Syncfusion® Java Word library (Essential® DocIO).
Take a moment to peruse the documentation. You can find basic Word document processing options along with features like mail merge, merge, and split documents.
Explore more about a rich set of Syncfusion® Word Framework features.
A GitHub example to perform a mail merge in a Word document in Java.