Recursive data file parsing?

I have a bunch of JSON files organized in subdirectories in the data folder.

data
 '- test1
     '- 20
         '- 06
            '- mycontent.json
            '- mycontent1.json
            '- mycontent2.json 
         '- 05
            '- myothercontent1.json
            '- myothercontent2.json

Basically they’re organized by test1 / year / month / content

When I tried to do

{{ range .Site.Data.test1 }}
    // do something
{{ end }}

In my template, it turns up empty but it shows up properly when the data files are moved to the top level folder data/test/*.

How can I recursively parse these data files that are buried in subdirectories?

In Hugo you can’t. There might be some Golang lingo that enables you to recursively scan a folder, but not in Hugo. This might be a question for the developers of Golang templates.

That’s unfortunate, but I’m glad I asked here instead of spending hours trying to figure it out.

Consider moving your data from data into a page bundle, and access them as page resources: https://gohugo.io/content-management/page-resources/

Hmm. I kind of would expect the above to just work. Can you try to put a dummy JSON file insde /20 and see what happens?

1 Like

Thanks I’ll give it a look once I wake up

It still doesn’t work. It only ever works when the data file is in the root of the path given

That is not true in general, so there must be something odd with your setup.

You can test it out → GitHub - s1-en/hugodatatest

When you build the site, you will only see the message from zero level depth, not 1 or 2 levels dee[per

Your data structure looks like this:

map[string]interface {}{
  "test":map[string]interface {}{
    "20":map[string]interface {}{
      "06":map[string]interface {}{
        "entry_1":map[string]interface {}{
          "test1":[]interface {}{
            "Hello from two level depth"
          }
        }
      },
      "entry_2":map[string]interface {}{
        "test1":[]interface {}{
          "Hello from one level depth"
        }
      }
    },
    "entry_3":map[string]interface {}{
      "test1":[]interface {}{
        "Hello from zero level depth"
      }
    }
  }
}

You could recursively get the value of test1 by replacing layouts/partials/test.html with:

{{- template "print-value-of-test1" . -}}

{{- define "print-value-of-test1" -}}
  {{- range . -}}
    {{- if reflect.IsMap . -}}
      {{- template "print-value-of-test1" . -}}
    {{- else -}}
      <li>{{ index . 0 }}</li>
    {{- end -}}
  {{- end -}}
{{- end -}}

There are probably other ways to accomplish this.

1 Like

Thank you so much! This wasn’t exactly what I needed but it was enough to push me in the right direction to figure out what I was doing wrong.

I checked out the Page Resource that maiki suggested but in the end I settled for the data approach.

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