# Extract JSON

**URL:** <https://discourse.gohugo.io/t/extract-json/21366>\
**Category:** support\
**Created:** [October 21, 2019, 3:11pm UTC](https://discourse.gohugo.io/t/extract-json/21366 "2019-10-21T15:11:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajasekar\_Sundaram](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rajasekar_sundaram/32/9795_2.png) [@Rajasekar\_Sundaram](https://discourse.gohugo.io/u/Rajasekar_Sundaram)\
**Post date:** [October 21, 2019, 3:11pm UTC](https://discourse.gohugo.io/t/extract-json/21366/1 "2019-10-21T15:11:47Z")

</div>

Hi All,

I have a JSON data something like below under the `content/post/post1`.

The links in the JSON changes for every post.

`urls.json`

```auto
{
    "url": [
        { "link": "https://google.com", "label": "Google" },
        { "link": "http://gohugo.io/", "label": "Hugo" },
     
    ]
}

```

How do I extract?

I tried

```
{{ range .Site.Dir.urls.url }}

```

But, it didn’t work.

Can some one help me.

---

<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:** [October 21, 2019, 4:31pm UTC](https://discourse.gohugo.io/t/extract-json/21366/3 "2019-10-21T16:31:37Z")

</div>

Give this a try:

```nohighlight
{{ $json := getJSON "content/post/post1/urls.json" }}

{{ range $json.url }}
  {{ . }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![Rajasekar\_Sundaram](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rajasekar_sundaram/32/9795_2.png) [@Rajasekar\_Sundaram](https://discourse.gohugo.io/u/Rajasekar_Sundaram)\
**Post date:** [October 21, 2019, 4:50pm UTC](https://discourse.gohugo.io/t/extract-json/21366/4 "2019-10-21T16:50:03Z")

</div>

Thank you for your response.

I dont know how to explain.

My url will be like

[example.com/post/post1](http://example.com/post/post1)  
[example.com/post/post2](http://example.com/post/post2)

In relative to data,

It will be

content/post/post1/index.md  
content/post/post2/index.md

My requirment is, every post will have different urls.json file under it.

`urls.json`

```auto
{
    "url": [
        { "link": "https://google.com", "label": "Google" },
        { "link": "http://gohugo.io/", "label": "Hugo" },
     
    ]
}

```

Need to extract them

---

<div class="post-metadata">

**Author:** ![frjo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/frjo/32/18626_2.png) [@frjo](https://discourse.gohugo.io/u/frjo)\
**Post date:** [October 21, 2019, 6:09pm UTC](https://discourse.gohugo.io/t/extract-json/21366/5 "2019-10-21T18:09:45Z")

</div>

See

> [@Loading JSON data file as a resource](https://discourse.gohugo.io/t/solved-loading-json-data-file-as-a-resource/15213/4):
>

and

> **[transform.Unmarshal](https://gohugo.io/functions/transform/unmarshal/)**
>
> Parses serialized data and returns a map or an array. Supports CSV, JSON, TOML, YAML, and XML.

---

<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:** [October 21, 2019, 7:21pm UTC](https://discourse.gohugo.io/t/extract-json/21366/6 "2019-10-21T19:21:00Z")

</div>

Then you need to build your path value per-page. For example, put something like this in your `layouts/_default/single.html` template:

```nohighlight
{{ $path := printf "content/%s/urls.json" .File.Dir }}

{{ $json := getJSON $path }}

{{ range $json.url }}
  {{ . }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![Rajasekar\_Sundaram](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rajasekar_sundaram/32/9795_2.png) [@Rajasekar\_Sundaram](https://discourse.gohugo.io/u/Rajasekar_Sundaram)\
**Post date:** [October 22, 2019, 3:13am UTC](https://discourse.gohugo.io/t/extract-json/21366/7 "2019-10-22T03:13:36Z")

</div>

Thanks Zachary.

Your code works for me.

One more thing.

is it possible? if the JSON file doesn’t exist, it should not display or not build.

In post 3, I don’t have a JSON file and it is giving below error.

Building sites … ERROR 2019/10/22 02:55:51 Failed to get JSON resource “content/post/post3/urls.json”: unexpected end of JSON input

For post1 and post2, I have no issues as they have JSON files.

Thanks.

---

<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:** [October 22, 2019, 4:22am UTC](https://discourse.gohugo.io/t/extract-json/21366/8 "2019-10-22T04:22:04Z")

</div>

> **[with](https://gohugo.io/functions/with#readout)**
>
> Rebinds the context (\`.\`) within its scope and skips the block if the variable is absent.

---

<div class="post-metadata">

**Author:** ![psmith1303](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/psmith1303/32/15469_2.png) [@psmith1303](https://discourse.gohugo.io/u/psmith1303)\
**Post date:** [January 15, 2022, 1:32am UTC](https://discourse.gohugo.io/t/extract-json/21366/9 "2022-01-15T01:32:31Z")

</div>

Did you ever get this working? I expect so, but if not, here is how I did it. First, I’m using page bundles, and I put the JSON file in the directory of the page bundle. Then I had to tweak your JSON file to look like this

```auto
[
    	"url": [
            { "link": "https://google.com", "label": "Google" },
            { "link": "http://gohugo.io/", "label": "Hugo" },
	]
]

```

Then I had a partial that did this:

```auto

{{- with .Resources.GetMatch "urls.json" -}}
  {{- $json := unmarshal .Content -}}
  {{- range $json -}}
  <ul>
     {{ range .url }}
       <li><a href="{{ .link }}">{{ .label }}</a></li>
     {{ end }}
  </ul>   
  {{- end -}}
{{- end -}}

```

That gave me a nice list of the links and and the labels … is that what you kind of wanted?
