# Ranging named values in a json

**URL:** <https://discourse.gohugo.io/t/ranging-named-values-in-a-json/41293>\
**Category:** support\
**Created:** [November 4, 2022, 1:35pm UTC](https://discourse.gohugo.io/t/ranging-named-values-in-a-json/41293 "2022-11-04T13:35:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xdk88](https://avatars.discourse-cdn.com/v4/letter/x/91b2a8/32.png) [@xdk88](https://discourse.gohugo.io/u/xdk88)\
**Post date:** [November 4, 2022, 1:35pm UTC](https://discourse.gohugo.io/t/ranging-named-values-in-a-json/41293/1 "2022-11-04T13:35:27Z")

</div>

Hello,

thanks to the help of @jmooring in [another thread](https://discourse.gohugo.io/t/using-variable-in-range-for-data-templates/40374) I was able to work around a problem I faced for a project. Another project now uses a similar structure and I need to dig into the json file another level deeper.

I’m using this within a shortcode. The shortcode call looks like this:  
`{{ game-popularity game="xygamename" monthyear="112022"}}`

Variables are declared within the shortcode:

```auto
{{ $game := .Get "game" }}
{{ $monthyear := .Get "monthyear" }}

```

But I can’t seem to get it to work with the additional variable. If I directly input the value of `$monthyear`, it works:

```auto
{{ range (index $.Site.Data.games.analysis $game).popularity.historical.112022 }}
{{ .average }},
{{ end }}

```

But how can I switch the “112022” to use the more flexible variable instead? Any ideas?  
Thanks!

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 4, 2022, 5:10pm UTC](https://discourse.gohugo.io/t/ranging-named-values-in-a-json/41293/2 "2022-11-04T17:10:13Z")

</div>

The `index` function is _variadic_ —it accepts a variable number of arguments.

Without an example of your data file I am only guessing, but I suspect you want something like:

```auto
{{ index site.Data.games.analysis "poker" "popularity" "historical" "112022" }}

```

---

<div class="post-metadata">

**Author:** ![xdk88](https://avatars.discourse-cdn.com/v4/letter/x/91b2a8/32.png) [@xdk88](https://discourse.gohugo.io/u/xdk88)\
**Post date:** [November 4, 2022, 5:23pm UTC](https://discourse.gohugo.io/t/ranging-named-values-in-a-json/41293/3 "2022-11-04T17:23:10Z")

</div>

Thanks, this code is working for me!  
But I’d like to use variables for at least two arguments (in this case they would in place of “poker” and “112022” since this is supposed to work as a shortcode. How would this line look with those two replaced by variables? Or is there something else/easier I could try apart from variables?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 4, 2022, 5:24pm UTC](https://discourse.gohugo.io/t/ranging-named-values-in-a-json/41293/4 "2022-11-04T17:24:21Z")

</div>

Just use a var instead of a literal string.

```auto
{{ index site.Data.games.analysis $game "popularity" "historical" $monthyear }}

```

---

<div class="post-metadata">

**Author:** ![xdk88](https://avatars.discourse-cdn.com/v4/letter/x/91b2a8/32.png) [@xdk88](https://discourse.gohugo.io/u/xdk88)\
**Post date:** [November 4, 2022, 5:32pm UTC](https://discourse.gohugo.io/t/ranging-named-values-in-a-json/41293/5 "2022-11-04T17:32:05Z")

</div>

Honestly, cannot believe I haven’t tried that before 😅  
Was so fixated on using the same scheme with the closing bracket for the index right behind the $game variable and tried multiple versions with quotes, but never moved the closing bracket.  
Thanks you very much (again)!

---

<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:** [November 6, 2022, 5:32pm UTC](https://discourse.gohugo.io/t/ranging-named-values-in-a-json/41293/6 "2022-11-06T17:32:39Z")

</div>

Closed on November 6, 2022, 5:32pm UTC

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