Comma in range; and making something optional

Hi,

I can’t figure this one … maybe someone can.

Structure

Person data is located in /data/persons/. The folder structure is:

/data/persons/amyrei.toml
/data/persons/yarso.toml

Desired output

schema.org json-ld

"founder": [
    {
        "@type": "Person",
        "@id": "https://example.com/Amyrei/#founder",
        "name": "Amyrei",
        "sameAs": ["https://example.net/Amyrei"],
        "url": "https://example.com/"
    },
    {
        "@type": "Person",
        "@id": "https://example.com/Yarso/#founder",
        "name": "Yarso",
        "sameAs": ["https://example.org/Yarso"],
        "url": "https://example.org/"
    }
],

Existing code

"founder": [ {{ range $p1, $p2 := $.Site.Data.persons }}{{ if eq $p2.role "founder" }}
    {
        role: {{ .role }}
        "@type": "Person",
        "@id": "{{ .homepage }}#founder",
        "name": "{{ .name }}",
        {{ with .sameas }}"sameAs": [{{ range $i, $v := . }}{{ if $i }}, {{ end }}"{{ $v }}"{{ end }}],{{ end }}
        "url": "{{ .homepage }}"
    }{{ end }}{{ end }}
],

This next code is similar in result as above:

"founder": [ {{ range $personskey, $personsmap := .Site.Data.persons }}
    {{- range $personkey, $personvalue := . -}}
    {{ with and (eq $personkey "role") (eq $personsmap.role "founder") }}
    {
        role: {{ $personsmap.role }}
        "@type": "Person",
        "@id": "{{ $personsmap.homepage }}#founder",
        "name": "{{ $personsmap.name }}"
    }
    {{- end }}
    {{- end -}}
{{ end }}
],

Roadblocks

Comma

If there is more than one "@type": "Person" there should be a comma.
- Since $personname is either Amyrei or Yarso, {{ if $personname -}}, {{ end }} won’t work.
- Tried: {{ cond (eq (len $personname) 1) "" ", " }} but of course it won’t work since $personname will always be more than 1.

“founder” key needs to be optional

I can not find a way to do it without repeating the “founder” key. Like in this code:

{{ range $n, $p := $.Site.Data.persons }}{{ if eq $p.role "founder" }}
"founder": [ {{ range $p1, $p2 := $.Site.Data.persons }}{{ if eq $p2.role "founder" }}
    {
        role: {{ .role }}
        "@type": "Person",
        "@id": "{{ .homepage }}#founder",
        "name": "{{ .name }}",
        {{ with .sameas }}"sameAs": [{{ range $i, $v := . }}{{ if $i }}, {{ end }}"{{ $v }}"{{ end }}],{{ end }}
        "url": "{{ .homepage }}"
    }{{ end }}{{ end }}
],
{{ end }}{{ end }}

If there are more than 1 Person-type, the whole “founder” key is repeated as well, it works as intended since the code is inside the range, but there can be only one “founder” key.