# Loading JSON data file as a resource

**URL:** https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213
**Category:** support
**Created:** [November 9, 2018, 7:59pm UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213 "2018-11-09T19:59:22Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![JamesTheHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jamesthehacker/32/6919_2.png) [@JamesTheHacker](https://discourse.gohugo.io/u/JamesTheHacker)
#### Post date: [November 9, 2018, 7:59pm UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213/1 "2018-11-09T19:59:22Z")

</div>

I have a JSON file stored in `content/operating-areas/areas.json`. There’s also an `index.md` within the `operating-areas` folder.

I am trying to load the `areas.json` file as a resource like so:

```
{{ with .Site.GetPage "operating-areas/index.md" }}
<section class="uk-section operating-areas">
  <div>
    <h2 class="uk-text-lead uk-text-center"><span>{{ .Title }}</span></h2>
    {{ with .Content }}
    <p class="uk-text-center">{{- . -}}</p>
    {{ end }}
  </div>

  <ul>
    {{ with .Resources.GetMatch "areas.json" }}
    {{ range .areas }}
    <li>{{ . }}<li>
    {{ end }}
    {{ end }}
  </ul>
</section>
{{ end }}

```

My JSON file:

```
{
    "areas": [
        "London",
        "Manchester",
        ...
    ]
}

```

But I’m getting an error:

`Building sites … ERROR 2018/11/09 19:25:04 Error while rendering "home" in "": template: index.html:7:5: executing "main" at <partial "operating-a...>: error calling partial: template: partials/operating-areas.html:12:13: executing "partials/operating-areas.html" at <.areas>: can't evaluate field areas in type resource.Resource`

Is it possible to load a data resource this way?

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [November 9, 2018, 8:46pm UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213/2 "2018-11-09T20:46:42Z")

</div>

There’s likely a better way to write this, but I think this will work for you for now:

```auto
<ul>
{{ $resource := (.Resources.GetMatch "areas.json").RelPermalink }}
{{ $data := getJSON "content" $resource }}
{{ range $areas := $data }}
    {{ range $area := $areas }}
    <li>{{ $area }}</li>
    {{ end }}
{{ end }}
</ul>

```

---

<div class="post-metadata">

### Author: ![JamesTheHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jamesthehacker/32/6919_2.png) [@JamesTheHacker](https://discourse.gohugo.io/u/JamesTheHacker)
#### Post date: [November 9, 2018, 9:30pm UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213/3 "2018-11-09T21:30:14Z")

</div>

> [@zwbetz](#):
>
> {{ $resource := (.Resources.GetMatch “areas.json”).RelPermalink }} {{ $data := getJSON “content” $resource }} {{ range $areas := $data }} {{ range $area := $areas }} \<li\>{{ $area }}\</li\> {{ end }} {{ end }}

Thanks this worked.

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [November 10, 2018, 12:56pm UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213/4 "2018-11-10T12:56:00Z")

</div>

> <https://github.com/gohugoio/hugo/issues/5428>

---

<div class="post-metadata">

### Author: ![ju52](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ju52/32/6749_2.png) [@ju52](https://discourse.gohugo.io/u/ju52)
#### Post date: [November 11, 2018, 7:38am UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213/5 "2018-11-11T07:38:03Z")

</div>

Hi @bep  
can you always **unmarshal** this?

From my old C++ view unmarshal should be under the surfarce as implementation detail.  
Normal user will not understand it. The PROs could use a .Raw property to access the unmarshalled data.

I started this comment with my understanding of marshalling. Could be I’m wrong with this.

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [November 11, 2018, 10:31am UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213/6 "2018-11-11T10:31:27Z")

</div>

> [@ju52](#):
>
> From my old C++ view unmarshal should be under the surfarce as implementation detail.

In my head there are two fundamental different ways to do this (correct me if I’m wrong. You can either:

1. Hide that “implementation detail” and create a more abstract API, aka. getJSON, getTOML, getCSV, getYAML etc.
2. Assume your users are note idiots, and give them the freedom to do whatever: `{{ resources.Get "my.json" | unmarshal }}`

In my head, 2) is better for everyone.

So, the _word_ **unmarshal** may be unfriendly/foreign for many, but it’s in so widespread use that I think we can use it: " **unmarshalling** or **unmarshaling** refers to the process of transforming a representation of an [object](https://en.wikipedia.org/wiki/Object_(computer_science)) that was used for storage or [transmission](https://en.wikipedia.org/wiki/Data_transmission) to a representation of the object that is [executable](https://en.wikipedia.org/wiki/Executable)."

> **[Marshalling (computer science)](https://en.wikipedia.org/wiki/Unmarshalling)**
>
> In computer science, marshalling or marshaling (US spelling) is the process of transforming the memory representation of an object into a data format suitable for storage or transmission, especially between different runtimes.\[citation needed\] It is typically used when data must be moved between different parts of a computer program or from one program to another. 
> Marshalling simplifies complex communications, because it allows using composite objects instead of being restricted to primitive ob...

So, in Hugo term: We have a `Resource` which is a string, but need to have it in a format that we can `range` over in the templates, so we unmarshal it.

But instead of me writing these long epos everytime the “why do it this way?”, I suggest you complete that sentence and explain to me “why not?” and “what would you recommend as an alternative approach?”

---

<div class="post-metadata">

### Author: ![ju52](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ju52/32/6749_2.png) [@ju52](https://discourse.gohugo.io/u/ju52)
#### Post date: [November 11, 2018, 11:09am UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213/7 "2018-11-11T11:09:19Z")

</div>

OK - was a question only

Why not - is this for 1. or 2. 😉

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [November 11, 2018, 1:26pm UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213/8 "2018-11-11T13:26:44Z")

</div>

> [@ju52](#):
>
> Why not - is this for 1. or 2. 😉

It is for your question in general. Why do you suggest that Unmarshal be hidden under the surface?

---

<div class="post-metadata">

### Author: ![ju52](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ju52/32/6749_2.png) [@ju52](https://discourse.gohugo.io/u/ju52)
#### Post date: [November 11, 2018, 1:43pm UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213/9 "2018-11-11T13:43:12Z")

</div>

-\> Wiki  
transforming the memory representation of an object to a data format suitable for **storage or transmission**

It was always hidden in API for RPC call (15 years ago). Old school 😉 .Net does it implicit if needed.

Do we need the unmarshalled data in Hugo?

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [November 11, 2018, 1:59pm UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213/10 "2018-11-11T13:59:22Z")

</div>

> [@ju52](#):
>
> Do we need the unmarshalled data in Hugo?

Certainly way more than you need, say, the JSON in text format. If you want to do something with JSON (or TOML etc.) and do not want to “hand parse” it (which is extremely unpractical, if at all possible, in Go templates), you need to unmarshal it.

If you want to quote Wikipedia, you need to copy and paste it, so no information gets lost. To me it looks like you mix marshal and unmarshal.

---

<div class="post-metadata">

### Author: ![ju52](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ju52/32/6749_2.png) [@ju52](https://discourse.gohugo.io/u/ju52)
#### Post date: [November 11, 2018, 2:35pm UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213/11 "2018-11-11T14:35:38Z")

</div>

I will not waste your time, IIRC:

marshal - from memory to storage or transmission  
unmarshal - from storage or transmission to memory

No need for me to discuss it here more - was only a question

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [November 11, 2018, 4:12pm UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213/12 "2018-11-11T16:12:51Z")

</div>

`+1` from me for this proposed way:

```
{{ resources.Get "my.json" | unmarshal }}
```

---

<div class="post-metadata">

### Author: ![HenrySkup](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/henryskup/32/8183_2.png) [@HenrySkup](https://discourse.gohugo.io/u/HenrySkup)
#### Post date: [April 24, 2019, 6:07am UTC](https://discourse.gohugo.io/t/loading-json-data-file-as-a-resource/15213/13 "2019-04-24T06:07:34Z")

</div>

I’m realising that this could be a way around [this issue on scanning through draft pages](https://discourse.gohugo.io/t/get-draft-front-matter-without-generating-page/18259/3).

and it is!

> **[transform.Unmarshal](https://gohugo.io/functions/transform.unmarshal/)**
>
> \`transform.Unmarshal\` (alias \`unmarshal\`) parses the input and converts it into a map or an array. Supported formats are JSON, TOML, YAML and CSV.
