Hugo slice to JSON array

Hi,
I can’t achieve a simple thing.

Render my Tags list as a JSON array.

How can we render tags = ["tag1", "tag2"]

to "tags": ["tag1", "tag2"].

Thanks a lot

top hit in docs search for JSON is encoding.Jsonify :wink:

just prepend the key for example using printf :

{{ $json := printf "\"tags\" = %s" (.Params.tags | jsonify) }}
1 Like

Thank you very much.

(The documentation is not friendly for a newbie like me with few examples, and read it for hours before posting.)

Finally, to transform a Hugo taxonomy (array or slice, icd10 in this example) to a JSON array:

[
{{- range $index, $value := where site.RegularPages "Section" "recommandations" -}}
  {{ if and ($index) (ne .Params.draft "True") }}, {{ end }}
  {
    "id": "{{ .Params.id }}",
    "permalink" : "{{ .Permalink }}",
    "title": "{{ .Title }}",
    "description": "{{ .Params.description }}",
    "icd10": {{ with .Params.icd10 }}{{ printf "%s" ( . | jsonify ) }}{{ else }}[]{{ end }},
    "date": "{{ .Date }}",
    "lastmod": "{{ .Lastmod }}",
    "contentHTML": {{ .Content | safeHTML | jsonify }}
  }
{{- end -}}
]

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.