📦 JSON Data
HTML;
// Process the JSON data
$body .= renderJsonData($payload);
$body .= <<
HTML;
$html = emailShell($emailTitle, $emailStamp, $body, "JSON data automatically parsed and formatted.");
$subject = "📊 JSON Webhook - " . $metadata['date'];
return [
'html' => $html,
'subject' => $subject
];
}
/**
* Recursively render JSON data as tables
* Sub-arrays are extracted into separate tables with path-based titles
*
* @param mixed $data The data to render
* @param string $path The current path (e.g., "Foo->Bar->Baz")
* @return string HTML representation
*/
function renderJsonData($data, $path = '') {
$html = '';
if (!is_array($data)) {
$safe = htmlspecialchars((string)$data, ENT_QUOTES, 'UTF-8');
return "
$safe
";
}
// Check if it's an indexed array (numeric keys in sequence)
$isIndexedArray = array_keys($data) === range(0, count($data) - 1);
if ($isIndexedArray && count($data) > 0) {
// It's an array of items
foreach ($data as $index => $item) {
$itemPath = $path ? $path . '->[' . $index . ']' : '[' . $index . ']';
if (is_array($item)) {
// Check if this item has any sub-arrays
$subArrays = [];
$simpleData = [];
foreach ($item as $key => $value) {
if (is_array($value)) {
$subArrays[$key] = $value;
} else {
$simpleData[$key] = $value;
}
}
// Render simple data for this item
if (!empty($simpleData)) {
if ($path) {
$html .= '
' . htmlspecialchars($itemPath, ENT_QUOTES, 'UTF-8') . '
';
}
$html .= '
';
$html .= '
Item ' . ($index + 1) . '
';
$html .= '
';
$html .= '
';
}
// Recursively render sub-arrays as separate tables
foreach ($subArrays as $key => $value) {
$subPath = $itemPath . '->' . $key;
$html .= renderJsonData($value, $subPath);
}
} else {
// Simple value in array
$valueSafe = htmlspecialchars((string)$item, ENT_QUOTES, 'UTF-8');
$html .= '
';
}
}
} else {
// It's an associative array (object-like)
// Separate simple values from arrays
$subArrays = [];
$simpleData = [];
foreach ($data as $key => $value) {
if (is_array($value)) {
$subArrays[$key] = $value;
} else {
$simpleData[$key] = $value;
}
}
// Render simple key-value pairs
if (!empty($simpleData)) {
if ($path) {
$html .= '
' . htmlspecialchars($path, ENT_QUOTES, 'UTF-8') . '
';
}
$html .= '
';
}
// Recursively render sub-arrays as separate tables with path titles
foreach ($subArrays as $key => $value) {
$subPath = $path ? $path . '->' . $key : $key;
$html .= renderJsonData($value, $subPath);
}
}
return $html;
}