JSON as data for websites

Hello
I am Stefano from Italy and i am happy to be here
i have a stupid question about HUGO, because i need select a right static site generetor that fit for my needs, so i need optimize time to learn, in a nutshell i can’t waste my time to learn something not usefull.

Can HUGO generate static sites start from json file? I have an ecommerce catalog products on some JSON file.

thanks

Yes, local blobs and remotely.

Can HUGO generate static sites start from json file?

No. You would need to write a script to generate the content pages from the JSON file. Also, you can use JSON as page front matter, so you don’t have to convert to TOML or YAML.

A couple issues related to the pages from data feature request:

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.

Hi there,
since there seems to still be no real solution I created a shell script with help of @alexandros … Maybe helps someone else trying to create several contentfiles from json…

#!/bin/bash

# You have to have jq installed locally for it to work

IFS=$'\n' # Each iteration of the for loop should read until we find an end-of-line

jq -c '.[]' ./assets/data/data.json | while read item; do
  dataName=$(jq -r '.dataName' <<< "$item")
  dataFolder=$(jq -r '.dataFolder' <<< "$item")
  if ! [ -z "$dataFolder" ]
  then
    dataFolderLower="$(tr [A-Z] [a-z] <<< "$dataFolder")"
    dataNameLower="$(tr [A-Z] [a-z] <<< "$dataName")"

    data123=$(jq -r '.data123' <<< "$item")
    data456=$(jq -r '.data456' <<< "$item")

    env HUGO_TITLE=${dataName} HUGO_DATA_123="${data123}" HUGO_DATA_456="${data456}" hugo new "test/${dataFolderLower}/${dataNameLower}.md"
  fi
done

unset IFS # Return IFS to its original value

… Something like this when working with custom subfolders as in my case…
Greetings :slight_smile: