docs/content/en/functions/hugo/Data.md
{{< new-in 0.156.0 />}}
Use the hugo.Data function to access data within the data directory, or within any directory mounted to the data directory. Supported data formats include JSON, TOML, YAML, and XML.
[!note] Although Hugo can unmarshal CSV files with the
transform.Unmarshalfunction, do not place CSV files in thedatadirectory. You cannot access data within CSV files using this method.
Consider this data directory:
data/
├── books/
│ ├── fiction.yaml
│ └── nonfiction.yaml
├── films.json
├── paintings.xml
└── sculptures.toml
And these data files:
- title: The Hunchback of Notre Dame
author: Victor Hugo
isbn: 978-0140443530
- title: Les Misérables
author: Victor Hugo
isbn: 978-0451419439
- title: The Ancien Régime and the Revolution
author: Alexis de Tocqueville
isbn: 978-0141441641
- title: Interpreting the French Revolution
author: François Furet
isbn: 978-0521280495
Access the data by chaining the identifiers:
{{ range $category, $books := hugo.Data.books }}
<p>{{ $category | title }}</p>
<ul>
{{ range $books }}
<li>{{ .title }} ({{ .isbn }})</li>
{{ end }}
</ul>
{{ end }}
Hugo renders this to:
<p>Fiction</p>
<ul>
<li>The Hunchback of Notre Dame (978-0140443530)</li>
<li>Les Misérables (978-0451419439)</li>
</ul>
<p>Nonfiction</p>
<ul>
<li>The Ancien Régime and the Revolution (978-0141441641)</li>
<li>Interpreting the French Revolution (978-0521280495)</li>
</ul>
To limit the listing to fiction, and sort by title:
<ul>
{{ range sort hugo.Data.books.fiction "title" }}
<li>{{ .title }} ({{ .author }})</li>
{{ end }}
</ul>
To find a fiction book by ISBN:
{{ range where hugo.Data.books.fiction "isbn" "978-0140443530" }}
<li>{{ .title }} ({{ .author }})</li>
{{ end }}
In the template examples above, each of the keys is a valid identifier. For example, none of the keys contains a hyphen. To access a key that is not a valid identifier, use the index function. For example:
{{ index hugo.Data.books "historical-fiction" }}