Grouping On Custom Data

I have a json file in the path \data\SampleFile.json with the content:

{
    "services": [
        {
            "name": "Service1"
        },
        {
            "name": "Service1"
        },
        {
            "name": "Service2"
        }
    ]
}

I would like to create a list of the unique values for name to do this I am trying to group the data like so:

{{ $serviceTypes := $.Site.Data.SampleFile.services | group "name" }}
{{ range $serviceTypes }}
    {{ .key }} <br>
{{ end }}

When I try to build the site using hugo v0.57.2 I get the error message:

Error: Error building site: "<path>\content\services\_index.md:8:1": 
failed to render shortcode "servicecatalogue/services": 
failed to process shortcode: "<path>\layouts\shortcodes\servicecatalogue\services.html:1:54": 
execute of template failed: template: shortcodes/servicecatalogue/services.html:1:54: 
executing "shortcodes/servicecatalogue/services.html" at <group "name">: 
error calling group: grouping not supported for type []interface {} *interface {}

Is grouping not supported for data files? Is there a better way to group using the data structure I have?

Here’s how I’d do it

{{ $data := site.Data.SampleFile }}
{{ $services := slice }}
{{ range $data.services }}
  {{ $services = $services | append .name }}
{{ end }}
{{ $services | uniq }}
2 Likes