Discussion
STC
IN
Last activity: 20 Jan 2026 9:10 EST
HTML Table Generation from Pega Data
We had a requirement to send a list of details in HTML table format to an external system. The external application maintains its own email/template engine and expects the content in a structured HTML table, not in JSON format.
-
In Pega, the data is received dynamically from an API and stored in a Page List structure.
-
The number of records is not fixed — sometimes it may contain 1 item, and in other scenarios 10 or more items.
-
Using the OOTB function @pxConvertPageToString(), we can easily convert the Page List into JSON.
-
However, the external system was not able to consume JSON, and explicitly requested the data in HTML table format so that they can directly embed it into their templates.
To meet this requirement, we implemented a custom Java-based utility to transform the JSON into an HTML table structure.
Implementation Steps in Pega:
-
Create a reusable function
-
Develop a Pega function that contains the custom Java logic to:
-
Parse the JSON generated from the Page List
-
Dynamically build an HTML
<table>with headers and rows -
Handle varying number of records gracefully
-
-
-
Invoke from Pega Rules
-
Call this function from:
-
Data Transform, or
-
Activity
-
-
Capture the returned HTML string and pass it to the external interface.
-
Sample Java code :
Create Function in PEGA :
Library Name : Create Librabry
Function Name: JsonToHTML
Input : jsonData [ String]
Output: Provide [String]
Java Snippet:
try {
com.fasterxml.jackson.databind.JsonNode root =
new com.fasterxml.jackson.databind.ObjectMapper().readTree(jsonData);
com.fasterxml.jackson.databind.JsonNode arr = null;
java.util.ArrayDeque<com.fasterxml.jackson.databind.JsonNode> stack =
new java.util.ArrayDeque<>();
stack.push(root);
while (!stack.isEmpty() && arr == null) {
com.fasterxml.jackson.databind.JsonNode node = stack.pop();
if (node != null && node.isArray() && node.size() > 0) {
arr = node;
break;
}
if (node != null && node.isObject()) {
java.util.Iterator<com.fasterxml.jackson.databind.JsonNode> it =
node.elements();
while (it.hasNext()) {
stack.push(it.next());
}
}
}
if (arr == null || arr.size() == 0) {
return "<table border='1'><tr><td>No data</td></tr></table>";
}
com.fasterxml.jackson.databind.JsonNode first = arr.get(0);
java.util.List<String> headers = new java.util.ArrayList<>();
java.util.Iterator<String> fn = first.fieldNames();
while (fn.hasNext()) {
headers.add(fn.next());
}
StringBuilder sb = new StringBuilder();
sb.append("<table border='1'>");
// Header logic
sb.append("<tr>");
for (String h : headers) {
sb.append("<th>")
.append(h == null ? "" :
h.replace("&","&")
.replace("<","<")
.replace(">",">")
.replace("\"",""")
.replace("'","'"))
.append("</th>");
}
sb.append("</tr>");
// rows logic
for (int i = 0; i < arr.size(); i++) {
com.fasterxml.jackson.databind.JsonNode row = arr.get(i);
sb.append("<tr>");
for (String h : headers) {
com.fasterxml.jackson.databind.JsonNode v = row.get(h);
String val =
(v == null || v.isNull()) ? "" : v.asText("");
sb.append("<td>")
.append(val.replace("&","&")
.replace("<","<")
.replace(">",">")
.replace("\"",""")
.replace("'","'"))
.append("</td>");
}
sb.append("</tr>");
}
sb.append("</table>");
return sb.toString();
} catch (Exception e) {
return "<table border='1'><tr><td>Error: "
+ e.getMessage() + "</td></tr></table>";
}