Bad output at the second map

Hello, I’m trying to create quizzes on Hugo.

For the moment I have chosen to use /data /quiz
My json looks like this :

{"query":
    [
        {"legend":"My query",
        "labels": [{
            "Ok":1,
            "Not Ok":10,
            "Dunno":100
        }]
        },    
        {"legend":"My 2nd query.",
        "labels": [{
            "Ok":1,
            "Not Ok":10,
            "Dunno":100
        }]
        }
    ]
}  

Note I added an int to the labels for the calculation of the result in JS later.

My template :

{{ define "main" }}
<div class="wrapper">
    <h1>{{ .Title }}</h1>
    {{ $urlJson := index .Site.Data.quiz .Params.urlJson }}
    
    <form action="">
    {{ range $i, $e := $urlJson}}
        {{ range $a, $b := $e }}
        <fieldset>
        <legend>{{.legend }}</legend>
            {{range $c, $d := .labels }}
                <label>{{ $d }}</label>
            {{end}}
        </fieldset>
        {{ end }}
    {{ end }}
    </form>
</div>   
{{ end }}

I can get the right exit on the
But I only display the map for
My output :

<form action="">
    
        
        <fieldset>
        <legend>My query</legend>
            
                <label>map[Dunno:100 Not Ok:10 Ok:1]</label>
            
        </fieldset>
        
        <fieldset>
        <legend>My 2nd query.</legend>
            
                <label>map[Dunno:100 Not Ok:10 Ok:1]</label>
            
        </fieldset>
        
    
    </form>

What do I miss about it?
I can modify my json if you think we can simplify it

Thanks,

well, you have an object within an array. so, that means that your range is only one index long, hence the single label.

I also think that you are using range … not “wrong” but maybe too much

is this what you are after?

(using this .json)

{"query":
    [
        {
            "legend":"My query",
            "labels": {
                "Ok":1,
                "Not Ok":10,
                "Dunno":100
            }
        },
        {
            "legend":"My 2nd query.",
            "labels": {
                "Ok":1,
                "Not Ok":10,
                "Dunno":100
            }
        }
    ]
}
<div class="wrapper">
    <h1>{{ .Title }}</h1>
    {{ $urlJson := index .Site.Data.quiz "query" }}

    <form action="">
    {{ range $urlJson}}
        <fieldset>
            <legend>{{.legend }}</legend>
                {{ range $key, $value := .labels }}
                    <label>{{ $key }}</label><br>
                {{ end }}
        </fieldset>
    {{ end }}
    </form>
</div>
2 Likes

Thanks Henry,
Kudos to you !

1 Like