How to remove trailing comma from last json-ld entry

currently I’m working on making a blog and a custom theme but I want to create a json-ld collectionpage schema that automatically ad a new blog entry.

I have created a custom json-ld with a range .data.pages and it works like it should, but there is 1 parsing issue.

To have a valid json-ld there should be no , at the last blog entry, my question: is there a way to check if the last entry is found and only then add a trailing slash at the end.

<script type="application/ld+json">
  {
    "@context": "http://schema.org",
    "@type": "CollectionPage",
    "headline": "{{ .Title }}",
    "url": "{{ .Permalink }}",

    "mainEntity":[
      {{ range .Data.Pages }}
        {
            "@type": "Article",
            "headline": "{{ .Title }}",
            "description": "{{ .Description }}",
            "url": "{{ .Permalink }}",
            "datePublished": "{{ .Params.date }}",
            "dateModified": "{{ .Params.mod_date }}",
            "author": {
                "@type": "Person",
                "name": "{{ .Site.Params.author }}",
                "url": "{{ .Site.Params.bio | absURL }}"
              },
              "image": {
                "@type": "ImageObject",
                "url": "{{ .Params.thumbnail | absURL }}"
              },
              "publisher": {
                "@type": "Organization",
                "name": "{{ .Site.Title }}",
                "url": "{{ .Site.BaseURL }}"      
              }
      
        }, <<-- this should be removed on the last entry


        {{ end }}
      
    ]
  
}
</script>
  
{{ end }}

I don’t know if there is a if statement that can check if the latest entry has reached or not.

Love to here if someone has a option to fix this.

Use this construct

{{ range $k, $_ := site.RegularPages.ByTitle }}
{{ if $k }},{{ end }}
  {{ .Title }}
{{ end }}
3 Likes