Unable to use conditions in JSON

I am trying to write a condition which checks for dateModified in front matter with {{- if not .Lastmod.IsZero }}...{{end}}, but for some reason, this still displays and shows last modified date as published date.

Example


<script type="application/ld+json">
{
        "headline": "{{ .Title }}",
        "url": "{{ .Permalink }}",
        "datePublished": "{{ .PublishDate.Format "2006-01-02T15:04:05-0700" | safeHTML}}",
        {{- if not .Lastmod.IsZero }}
        "dateModified": "{{ .Lastmod.Format "2006-01-02T15:04:05-0700" | safeHTML}}",
        {{ end -}}
}
</script>

I tried using {{ .Page.Lastmod }} and it still shows up.

It depends on your config file

.LastMod has always a defined value!
try

[frontmatter]
lastmod                    = [ "lastmod"]

to use the frontmatter value only