JSON as data for websites

Some time ago I posted this technique about creating separate pages from a CSV file.

Here is the JSON variation:

Sample file contents of test.json that needs to reside under the assetDir

[
  {
    "filename": "foo",
    "title": "bar"
  }
]

Sample archetype that needs to be created in archetypes/test.md

{{- with resources.Get "test.json" -}}
{{- $content := .Content | transform.Unmarshal -}}
{{- range $content -}}
{{- if eq (getenv "HUGO_TITLE") .filename -}}
+++
title = "{{ .title }}"
{{- end -}}
{{- end -}}
{{- end }}
lastmod = "{{ dateFormat "Monday, Jan 2, 2006, 15:04:05 EEST" .Date }}"
+++

Sample CLI command that uses enviroment variables and outputs a markdown file if the environment variable HUGO_TITLE is present in the .filename value of a JSON object.

env HUGO_TITLE="foo" hugo new "test/foo.md"

End result of foo.md

+++
title = "bar"
lastmod = "Thursday, Apr 2, 2020, 13:52:51 EEST"
+++

So until the aforementioned GitHub issues are resolved this is the best way to create pages from data files in Hugo without external scripts.