From b60448bc771920fb2e7fabb6c39ef7973696d5bb Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Wed, 25 Mar 2026 15:11:05 +0100 Subject: [PATCH] Fix leading comma in JSON export (#1349) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When exporting JSON with a limit and a page > 1 set, a leading comma is added before the first item because `$offset !== 0` evaluates to true, resulting in invalid JSON. Replace the offset‑based condition with a `$first` flag to track whether any item has been written yet. --- library/Icingadb/Data/JsonResultSetUtils.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/library/Icingadb/Data/JsonResultSetUtils.php b/library/Icingadb/Data/JsonResultSetUtils.php index 39a0a3fe..96883f9e 100644 --- a/library/Icingadb/Data/JsonResultSetUtils.php +++ b/library/Icingadb/Data/JsonResultSetUtils.php @@ -85,13 +85,16 @@ trait JsonResultSetUtils $offset = 0; } + $first = true; echo '['; do { $query->offset($offset); $result = $query->execute()->disableCache(); - foreach ($result as $i => $object) { - if ($i > 0 || $offset !== 0) { + foreach ($result as $object) { + if ($first) { + $first = false; + } else { echo ",\n"; }