# Group data from CSV file

**URL:** <https://discourse.gohugo.io/t/group-data-from-csv-file/40159>\
**Category:** support\
**Created:** [August 24, 2022, 9:22am UTC](https://discourse.gohugo.io/t/group-data-from-csv-file/40159 "2022-08-24T09:22:05Z")\
**Posts on this page:** 1\
**Showing post:** 3

<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:** [August 24, 2022, 5:33pm UTC](https://discourse.gohugo.io/t/group-data-from-csv-file/40159/3 "2022-08-24T17:33:35Z")

</div>

It would be simpler if your data were in JSON, TOML, or YAML format, but if you’re stuck with CSV…

foo.csv

```nohighlight
name,type
Felix,cat
Spot,dog
Rover,dog

```

template

```auto
{{/* Wrangle the CSV into a slice of objects. */}}
{{ $csv := getCSV "," "foo.csv" }}
{{ $cols := index $csv 0 }}
{{ $data := slice }}
{{ range $k, $v := $csv }}
  {{ if $k }}
    {{ $m := dict }}
    {{ range seq 0 (sub (len $cols) 1) }}
      {{ $m = merge $m (dict (index $cols .) (index $v .)) }}
    {{ end }}
    {{ $data = $data | append $m }}
  {{ end }}
{{ end }}

{{/* Build unique, sorted slice of types. */}}
{{ $types := slice }}
{{ range $data }}
  {{ $types = $types | append .type }}
{{ end }}
{{ $types = $types | uniq | sort }}

{{/* Display the grouped data. */}}
<ul>
  {{ range $types }}
    <li>
      {{ . }}
      <ul>
        {{ range where $data "type" . }}
          <li>{{ .name }}</li>
        {{ end }}
      </ul>
    </li>
  {{ end }}
</ul>

```

result

![image](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/3X/9/e/9e3cfa677cf206805fdc5c16e7289c879291293b.png)

---

_[View the full topic](https://discourse.gohugo.io/t/group-data-from-csv-file/40159)._
