Ranging named values in a json


thanks to the help of @jmooring in another thread 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:

{{ $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:

{{ 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?

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:

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

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?

Just use a var instead of a literal string.

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

Honestly, cannot believe I haven’t tried that before :sweat_smile:
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)!

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