A simple one: how to read a nested list into json

For example, to read from yaml front matter this list:

tags: ["a", "b"]

It works to use in index.json:

    "tags":                      
    {{- with .Params.tags -}}
      {{ delimit . ", " | jsonify }}   
    {{- else -}} 
	    "" 
    {{- end -}}

Result:

"tags":"a, b"

My attempt to read:

technologies: [["a1", "b1", "c1"], ["a2", "b2", "c2"]]

with:

   "technologies":                      
    {{- with .Params.technologies -}}
      {{range . }}
        {{ delimit . ", " }}   
      {{end}}
    {{- else -}} 
	    "" 
    {{- end -}}

Gives:

    "technologies":
        a1, b1, c1   
   
        a2, b2, c2   

I would be satisfied with:

        "technologies": "a1, b1, c1, a2, b2, c2"

Wrapping in another range gave gibberish numbers between the lines.
Any hints are welcomed!

You have nested lists so you could using append to flatten the list and then delimit it:

 {{ with .Params.Technologies }}
    {{ $merged := slice }}
    {{ range . }}
       {{ $merged = $merged | append .}}
    {{ end }}
<!-- direct output to page -->
    "technologies": "{{ delimit $merged ", " }}"</br>
<!-- or create json string -->
   {{ $object := jsonify (dict "technologies" (delimit $merged ", ")) }}
<!-- and render it -->
   {{ highlight $object "JSON"}}
 {{end}}
1 Like

Neet! It worked, thank you! :+1: :+1:
I had to place “technologies” above everything:

    "technologies": 
        {{ with .Params.technologies }}
            {{ $merged := slice }}
            {{range . }}
                {{ $merged = $merged | append .}}
            {{end}}
            {{ delimit $merged ", " | jsonify}}
        {{- else -}} 
            "" 
        {{end}}

Otherwise got json parse error, when page has no “technologies” json output: "tags":"..",
So he minds the trailing comma. But this is ok: "tags":"", technologies:""

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