# Access Content markdown file based on filename obtained from json Data file

**URL:** <https://discourse.gohugo.io/t/access-content-markdown-file-based-on-filename-obtained-from-json-data-file/32196>\
**Category:** Uncategorized\
**Created:** [April 6, 2021, 2:24pm UTC](https://discourse.gohugo.io/t/access-content-markdown-file-based-on-filename-obtained-from-json-data-file/32196 "2021-04-06T14:24:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wang](https://avatars.discourse-cdn.com/v4/letter/w/edb3f5/32.png) [@wang](https://discourse.gohugo.io/u/wang)\
**Post date:** [April 6, 2021, 2:24pm UTC](https://discourse.gohugo.io/t/access-content-markdown-file-based-on-filename-obtained-from-json-data-file/32196/1 "2021-04-06T14:24:59Z")

</div>

Hello, I have a .json Data file, which contains a string array with slugs, let’s say `["red", "green", "blue"]`.

In my content directory I have markdown files like this: `content/favorite/colors/red.md`

When I loop over the `["red", "green", "blue"]` array that I accessed from the .json file, how can I load the content markdown file in order to access its parameters and text? I’m only interested in loading the content from say 3 markdown files out of 1000, so would ideally fetch the file directly without any loop.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [April 6, 2021, 2:30pm UTC](https://discourse.gohugo.io/t/access-content-markdown-file-based-on-filename-obtained-from-json-data-file/32196/2 "2021-04-06T14:30:54Z")

</div>

SHORT: getPage and printf 😉

LONG:

```auto
{{ $yourJSONData := slice "red" "green" "blue" }}
{{ range $yourJSONData }}
  {{ $thePage := printf "favorite/colors/%s" . }}
  {{ $thePageYouWant := getPage $thePage }}
  {{ with $thePageYouWant }}
    Title: {{ .Title }}
    Content: {{ .Content }}
  {{ end }}
{{ end }}

```

- you range through whatever part of your data configuration. I did a quick slice-collection here.
- inside of the first range the dot (`.`) is one of your favourite colours
- you print a path to the files with your favourite colours
- the `getPage` “gets the page” and returns a page object
- the with takes the page and puts it into the dot (`.`)
- do your stuff with the page
- move on to the next

> **[.GetPage](https://gohugo.io/functions/getpage/#readout)**
>
> Gets a \`Page\` of a given \`path\`.

> **[printf](https://gohugo.io/functions/printf/#readout)**
>
> Formats a string using the standard \`fmt.Sprintf\` function.

---

<div class="post-metadata">

**Author:** ![wang](https://avatars.discourse-cdn.com/v4/letter/w/edb3f5/32.png) [@wang](https://discourse.gohugo.io/u/wang)\
**Post date:** [April 6, 2021, 2:48pm UTC](https://discourse.gohugo.io/t/access-content-markdown-file-based-on-filename-obtained-from-json-data-file/32196/3 "2021-04-06T14:48:36Z")

</div>

Thanks for the fast reply, the code is really great, and exactly what I need 🙂  
There’s one problem though, I for some reason get `nopPage` when I try to show the content of `{{ $thePageYouWant }}` even though I’m certain the page exists (if I open e.g. localhost:1313/favorite/colors/red/ then it will load the content of the red.md file).

When I do printf then I can also see the path is correct: I’ve tried where `{{ $thePage }}` returned:

- /favorite/colors/red.md
- favorite/colors/red.md
- /favorite/colors/red
- favorite/colors/red

Based on the documentation you linked, I think `/favorite/colors/red.md` would be correct, but not sure… I should probably also mention that in my project I support multiple languages, I’m not sure if that could have any impact, but I don’t think so since I use the full path which include the language (e.g. /en/favorite/colors/red.md).

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [April 6, 2021, 3:02pm UTC](https://discourse.gohugo.io/t/access-content-markdown-file-based-on-filename-obtained-from-json-data-file/32196/4 "2021-04-06T15:02:05Z")

</div>

The one thing I never know about is multilingual sites, but the docs say you should try the slash at the beginning and no file extension at the end:

> **[.GetPage](https://gohugo.io/functions/getpage/#getpage-and-multilingual-sites)**
>
> Gets a \`Page\` of a given \`path\`.

---

<div class="post-metadata">

**Author:** ![wang](https://avatars.discourse-cdn.com/v4/letter/w/edb3f5/32.png) [@wang](https://discourse.gohugo.io/u/wang)\
**Post date:** [April 6, 2021, 3:09pm UTC](https://discourse.gohugo.io/t/access-content-markdown-file-based-on-filename-obtained-from-json-data-file/32196/5 "2021-04-06T15:09:08Z")

</div>

Ahh great, the problem was that I included the language in the file path, so it became e.g. `/en/favorite/colors/red` but removing the language so it was simply `/favorite/colors/red` worked. Thanks a lot 🙂

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [April 8, 2021, 3:09pm UTC](https://discourse.gohugo.io/t/access-content-markdown-file-based-on-filename-obtained-from-json-data-file/32196/6 "2021-04-08T15:09:54Z")

</div>

Closed on April 8, 2021, 3:09pm UTC

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