Not Getting keys from Json file using delimit function

Hi there,
I have json file like this :

{
   "hi-core/bower": {
       "name": "hi-core",
       "dependencies": {
            "showdown": "1.8.6",
           "moment": "2.22.1"
       }
}

I am trying to fetch the dependencies using a shortcode,which i am getting but i am getting like this:

map[showdown-“1.8.6”]

I used delimit function so that map goes away but 'delimit ’ only gives me the value of the key and not the key itself(i.e just the ‘showdown’)!

Is there any way i can just get the keys of ‘dependencies’?

Any help is appreciated!
Thanks!

You need to use range.

If the map map[showdown-“1.8.6”] is stored in $foo, do:

{{ range $key,$val := $foo }}
    <!-- $key will then hold "showdown" -->
    <!-- $val will hold "1.8.6" -->
{{ end }}

“map[showdown-“1.8.6”]” is just how Go represents the map in string form… it’s not literally stored like that.

1 Like

That worked for me! Thanks :slight_smile:

@kaushalmodi Is it true that i cant use delimit function with range?

I am trying to do this but looks like it doesnt work

<td> 
{{ range $key,$val := $cur }} 
{{ delimit ($key) ", "  }} 
{{ end }}
</td>

Thanks!

It depends what you are applying the delimit function to. It can be applied only to slices/maps. $key in that example is a string; it won’t work there.

See https://gohugo.io/functions/delimit/.

May be you need to present that example JSON file and intended output, and ask what Go templating would be needed to go from that JSON to that output.