Problem with data file variable name: "bad character U+002D"

I am working on a shortcode to list a bibliography.

The JSON data file is converted from a BibTeX file. One of the standard fields is “container-title”

When I try to read that file, Hugo throws an error as dashes are not allowed in variable names.

I read the solutions using the index function for parameters. How can I fix this issue when reading a JSON file?

My workaround is to rename the variable in the script after export.

Hello @danderzei

The error log is pretty clear, you can’t use space in a variable name. you can use urlize if possible.

Thanks for responding. urlize cleans the contents of variables, not names.

I understand the naming conventions. Is there a way to escape the banned symbols, like in many other programming languages?

You can use replaceRE to escape any character or symbols

Indeed, that replaces the content of the variable, but not the name.

I have a variable in a JSON file named container-title. I cannot display this variable with {{ .container-title }} because it throws the error. replaceRE or urlize only work with the output of the variable, not the name.

In other languages, I can escape the dash by, for example, using quotation marks. Does Hugo have a similar option?

That makes little sense. JavaScript (and by extension JSON) does not allow dashes in variable names: | Variables
So what does your JSON really look like?

The JSON is a BibTeX file converted to JSON. One entry in the file looks like this (note the container-title variable):

    "DOI": "10.1016/j.shpsa.2009.10.006",
    "author": [
        "family": "Al-Gailani",
        "given": "Salim"
    "container-title": "Studies In History and Philosophy of Science",
    "id": "al-gailani_magic_2009",
    "issue": "4",
    "issued": {
      "date-parts": [
    "keyword": "Gender, Conjuring",
    "page": "372-381",
    "title": "Magic, science and masculinity: Marketing toy chemistry sets",
    "title-short": "Magic, science and masculinity",
    "type": "article-journal",
    "volume": "40"

This is part of the Hugo code I am working on:

{{ $kwd := .Get 0 }}

<div class ="box">
  {{ range .Site.Data.bibliography }}
  {{ if in (lower .keyword) (lower $kwd) }}

    {{ if or (eq .type "book") (eq .type "thesis") }}
    <i>{{title .title }}</i> ({{ .publisher }}).
    {{ else }}
    {{ .title }}. <i>{{ title .container-title }}</i>{{ if .volume }}, {{ .volume }}{{ end }}{{ if .issue }}({{ .issue }}){{ end }}{{ if .page }}, {{ .page }}{{end}}.
    {{ end}}
    {{ if .DOI }}doi: <a href = "{{ .DOI }}" target="_blank">{{ .DOI }}</a>.{{ end }}
    {{ if .URL }}<a href = "{{ .URL }}" target="_blank">{{ trim .URL "https://" }}</a>.{{ end }}

  {{ end }}

  {{ end }}

I do a lot of R programming, in that language I can escape variable names with backticks so that very character is allowed.

If that is impossible in Hugo, than that is fine. I wrote a script to clean the variable names. But ideally, I would do it in Hugo instead of Bash.

This (container-title) is not a variable name. It is the name of a property. Also, your error description is lacking a bit. Apparently, the problem does not when you read the JSON file but when you try to access the property (which has been converted to a key in a map by Hugo when it read the file). Obviously, a dash in a variable name is not allowed in Hugo.

However, index . "container-title" might give you what you want, provided that . contains the map.

1 Like

U+002D is not a space.

Thanks for the solution chrillek

Your solution works a treat and I have learnt something :slight_smile:

1 Like

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