Strip comma from last element in JSON string for Schema Markup

I am creating JSON+LD markup for my opencart. I’m using the repeater for breadcrumbs and it’s working wonderfully, however to get valid markup I need to remove the comma from the last repeater/breadcrumb item. Here’s my code:

<script type=“application/ld+json”>
@context”: “”,
@type”: “CollectionPage”,“breadcrumb”:
{"@type": “BreadcrumbList”,“ItemListElement”:
<?php $crumbly = 0; ?>
<?php foreach ($breadcrumbs as $breadcrumb):
{"@type":“ListItem”,“position”:<?php echo $crumbly; ?>,“item”:
{"@id":"<?php echo $breadcrumb[‘href’]; ?>",“name”:"<?php echo $breadcrumb[‘text’]; ?>"}},
<?php endforeach ?>

The result if there were just two breadcrumb items (minus the comma on the last item) should be:

<script type=“application/ld+json”>
{"@id":“",“name”:"Home”}}, // <-- Comma required because there’s another list item to follow this one.
{"@id":“",“name”:"MyCategory”}} // <-- No comma required as this is the last list item. However, as per my code, it will show a comma here.

Please advise how I can modify my script. Thanks in advance!

If you happen to be already using the Kirby meta tags plugin, it now supports JSON schema.

See this post for how to do it.