json.json

Hugo supports JSON outputs out of the box. But to utilize it, you should define an output template based on the data available to your page kinds and any frontmatter included in the markdown file.

This partial is used in the following default layouts:

Each of these outputs can be found by adding /index.json to the path of the home, section, or single page.

How it works

By itself, this partial doesn’t do anything. When piped reference from a few of our default layouts, it acts as a standardized blueprint for how each article should look like in json. Updating this file will cascade to all outputs where this is referenced.

Source code

{
    "id": "{{- .File.UniqueID -}}",
    "title": "{{- if .Title}}{{- .Title }}{{else}}{{- humanize .File.TranslationBaseName -}}{{- end}}",
    "description":{{- with .Description}} {{. | jsonify}}{{- else }}"no description"{{- end}},
    "lastCommit": "{{ .GitInfo.AuthorDate }}",
    "version": "{{- if .Page.Params.version.isLatest}}latest{{else}}{{- .Page.Params.version.major }}.{{- .Page.Params.version.minor }}.{{- .Page.Params.version.patch }}{{- end}}",
    "section": {{- with .Section }}{{. | jsonify}} {{- else }}"no section"{{- end}},
    "parent": "{{with .Parent}}{{- .Title }}{{- else }}no parent{{end}}",
    "isPage": {{- .IsPage | jsonify }},
    "isSection": {{- .IsSection | jsonify }},
    "pageKind": {{- .Kind  | jsonify }},
    "bundleType": {{- .BundleType | jsonify }},
    "uri": "{{- .Permalink }}",
    "relURI": "{{- .RelPermalink }}",
    "body": {{ .Plain | jsonify }},
    {{- if .Page.Params.hidden}}"hidden": "{{- .Params.hidden}}",{{- end}}
    "tags": [{{- range $tindex, $tag := .Params.tags }}{{- if $tindex }}, {{- end }}"{{- $tag| htmlEscape -}}"{{- end }}]
}