Extracting Specific Value from a Nested Json Data File

Hi All,
I’m new to Hugo so please pardon me if this question was already answered. I did try looking around discourse quite a bit but wasn’t able to find an answer.

I created a data file called quotes.json and it’s contents are:
{ “Robert” : {
“quote” : “Quote1”,
“name” : “Robert Heinlein”,
“book” :“Time Enough for Love”
},
“Charles” : {
“quote” : “Quote2”,
“name” : “Charles Buckowski”,
“book” : “Post Office”
}
}

Inside my layouts folder, I have an html file that does:
{{ range .Site.Data.quotes }}
{{ .quote}}
{{end}}

However, this displays value of “quote” for both “Robert” and “Charles”. How can I define it such that it only shows the value of the author I choose - for instance “Robert”.

Do I need to define a value in the front matter? Any assistance is much appreciated.

Thanks,
Shash

Do you mean you’d like to only show the quotes of a single author?

Also, it isn’t clear from your example which front matter you are referring to. Please share a sample project showing your project structure. :slight_smile:

{{ $quotes := .Site.Data.quotes }}
{{ with index $quotes "charles" }}
  {{ .name }}
{{ end }}

I think this is case agnostic, so “Charles” or “charles” should both work, but not sure.

1 Like

Worked like a charm! This just helped me learn about index as well. Thanks a ton mate!

1 Like