Archetypes can be specified also in JSON format

The documentation page for archetypes illustrates their usage with YAML, the minimal example being:

---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
---

but while not mentioned in the docs, in fact hugo will accept archetypes specified in JSON (probably TOML as well but I haven’t tested it). E.g. something like:

{
  "title": "{{ replace .File.ContentBaseName '-' ' ' | title }}",
  "date": "{{ .Date }}",
  "draft": true
}

will work fine. It seems that when generating content with hugo new --kind archetype, hugo will simply propagate the archetype format it finds into the newly generated page’s front matter (if the archetype is in json, the front matter will be in json).

This should be primarily useful for people who want to manage their front matter with json tools.

3 Likes

Maybe you can create an issue recommending an update to the docs https://github.com/gohugoio/hugoDocs/issues

2 Likes

I’m working on an update now.

2 Likes

This will not work. You must replace the single quotes with backticks:

{
  "title": "{{ replace .File.ContentBaseName `-` ` ` | title }}",
  "date": "{{ .Date }}",
  "draft": true
}

I’ve updated the documentation:
https://gohugo.io/content-management/archetypes/

1 Like

Thanks for the update!

Re quotes: it works for me as is (hugo v0.113 on linux).

Poor word choice on my part. It works, but the JSON is invalid (unescaped quotes). Thanks for the documentation nudge; changes to that page were long overdue.

1 Like