Data files, json & dynamic keys

According to https://gohugo.io/templates/data-templates/, JSON data can be accessed using .Site.Data.[filename].key.

In the JSON world, the Key is sometimes part of the data, and we need to be able to retrieve the key name dynamically.

What syntax allows me to access the names of keys dynamically?

Here’s an example of a JSON fragment for a tournament score publication utility. The data has the competitor’s name as a key, and their scores as an array value.

comptition: [
  {
    "fred binkey": [12, 15, 28, 22, 3],
    "joan watson": [30,29,23,21,28]
  },
  {
    "sam clemens": [22,4,21,3,30],
    "sherlock holmes": [29,28,30,30,26]
  }
]

In my output, I want to show the name of the competitor, and then their scores.

See the index func: https://gohugo.io/functions/index-function/

I’m not convinced that’s it. If I look at that doc, I see that if I wanted to get at “sam clemens” data, I might use:

{{ index .competition 1 "sam clemens" }}

What I want in reality is to get a the name of the first key in the second item:

{{ index .competition 1 }} ==> "sam clemens"?

Anyway, I saw that my schema was flawed and fixed both problems.

What I actually wanted was the $index parameter to range.