Pull specific value/string from a json data file?

I created a json file and put it into my data folder. It has two value/string entries:

"ERIS": {"name": "Equipment Recycling In-Shop"}
"ERAL": {"name": "Equipment Recycling Pickup At-Location"}

Is there a way to pull specific values/strings from a json file? I would like to store attributes for specific options for any of my services. The documentation talks about raging though all the values, but I want to use it to store data and pull specific values/strings from the json file. This way I dont have to replicate this data by copying/pasting on each page that I would like to use these values in. Is this not a good way to use the data templates?

Ok I found the similar question, but it relates to front matter, can you specify a fuction call in the content part of a file?

{{ index .Site.Data.service-location.ERIS.name }}

It doesn’t seem to work.

If that’s all you have in your JSON file, it won’t work since that’s not valid JSON. You’re missing outside curly brackets and a comma. Try using an online JSON validator.

Thank you, I fixed my json and validated it:

{
	"ERIS": {"name": "Equipment Recycling In-Shop"},
	"ERAL": {"name": "Equipment Recycling Pickup At-Location"}
}

Should I expect to be able to pull a value from that file now using the following code?

{{ index .Site.Data.service-location.ERIS.name }}

The value does not render in my content markdown file.

So, the example linked here doesn’t help? :

My understanding based on what I have read there is that there are examples for ranging though the values in a specific file, but I just want to pull specific values from a json file, did i miss something?

Sorry, I meant to link this:

There’s an example there that specifies what string to index:

 <div>Short Description of {{.Site.Data.User0123.Name}}: 
 <p>{{ index .Site.Data.User0123 "Short Description" | markdownify }}</p></div>

Can you adapt?

Also this:

Ok, I tested this and, got it working. Using --verbose on hugo server was key though. Turns out, Hugo did not like having a hyphen in the doc name. That caused an error.

So, given /data/servicelocation.json (no hyphen) with your content:

{
	"ERIS": {"name": "Equipment Recycling In-Shop" },
	"ERAL": {"name": "Equipment Recycling Pickup At-Location" }
}

I can pinpoint ERIS’s “name” with this:

{{ index .Site.Data.servicelocation.ERIS.name }}

… in my single.html. To see the relevant maps, try also:

{{ index .Site.Data.servicelocation }}
{{ index .Site.Data.servicelocation.ERIS }}

That shows you what you’re working with.

Thank you so much Rick for testing this out. So two things I noticed:

  1. I had my service-location.json file located in a sub directory called templates thinking that would be the root where the data files would be accessed using:

{{ index .Site.Data.service-location.ERIS.name }}

  1. I removed the hypen in the file name, copied the json file to the root of my data directory: /data/servicelocation.json and also changed:

{{ index .Site.Data.servicelocation.ERIS.name }}

For some reason it is still not rendering. I looked at the --verbose output and I did not noticed an error related to the index command outputting the string:

c:\Users\trinsic\GitHub\scottrlarson\profession>hugo server --verbose
INFO 2018/02/20 12:34:35 Using config file: c:\Users\trinsic\GitHub\scottrlarson
\profession\config.toml
INFO 2018/02/20 12:34:35 using a UnionFS for static directory comprised of:
INFO 2018/02/20 12:34:35 Base: c:\Users\trinsic\GitHub\scottrlarson\profession\t
hemes\academic\static
INFO 2018/02/20 12:34:35 Overlay: c:\Users\trinsic\GitHub\scottrlarson\professio
n\static\
INFO 2018/02/20 12:34:35 syncing static files to c:\Users\trinsic\GitHub\scottrl
arson\profession\
Started building sites ...
WARN 2018/02/20 12:34:36 Failed to read data from templates\examples-shorecode.t
xt\examples-shorecode.txt: Data not supported for extension 'txt'
WARN 2018/02/20 12:34:36 Data for key 'mono_font' in path 'fonts\default.toml' i
s overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'font_size' in path 'fonts\default.toml' i
s overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'font_size_small' in path 'fonts\default.t
oml' is overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'name' in path 'fonts\default.toml' is ove
rridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'google_fonts' in path 'fonts\default.toml
' is overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'heading_font' in path 'fonts\default.toml
' is overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'body_font' in path 'fonts\default.toml' i
s overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'nav_font' in path 'fonts\default.toml' is
 overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'light' in path 'themes\dark.toml' is over
ridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'menu_primary' in path 'themes\dark.toml'
is overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'name' in path 'themes\dark.toml' is overr
idden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'primary' in path 'themes\dark.toml' is ov
erridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'menu_text' in path 'themes\dark.toml' is
overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'primary_light' in path 'themes\dark.toml'
 is overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'primary_dark' in path 'themes\dark.toml'
is overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'menu_text_active' in path 'themes\dark.to
ml' is overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'menu_title' in path 'themes\dark.toml' is
 overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'home_section_odd' in path 'themes\dark.to
ml' is overridden in subfolder
WARN 2018/02/20 12:34:36 Data for key 'home_section_even' in path 'themes\dark.t
oml' is overridden in subfolder
INFO 2018/02/20 12:34:36 found taxonomies: map[string]string{"category":"categor
ies", "publication_type":"publication_types", "tag":"tags"}
INFO 2018/02/20 12:34:36 Alias "\services\information\security\page\1\index.html
" translated to "services\information\security\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\services\computer\assistance\page\1\index.html"
 translated to "services\computer\assistance\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\services\data\page\1\index.html" translated to
"services\data\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\services\computer\home-office\page\1\index.html
" translated to "services\computer\home-office\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\services\computer\multimedia-gaming\page\1\inde
x.html" translated to "services\computer\multimedia-gaming\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\services\computer\repair\page\1\index.html" tra
nslated to "services\computer\repair\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\resources\publications\page\1\index.html" trans
lated to "resources\publications\page\1\index.html"
WARN 2018/02/20 12:34:36 [en] Unable to locate layout for "taxonomyTerm": [taxon
omy/category.terms.en.html.html taxonomy/category.terms.html.html taxonomy/categ
ory.terms.en.html taxonomy/category.terms.html _default/terms.en.html.html _defa
ult/terms.html.html _default/terms.en.html _default/terms.html indexes/indexes.e
n.html.html indexes/indexes.html.html indexes/indexes.en.html indexes/indexes.ht
ml theme/taxonomy/category.terms.en.html.html theme/taxonomy/category.terms.html
.html theme/taxonomy/category.terms.en.html theme/taxonomy/category.terms.html t
heme/_default/terms.en.html.html theme/_default/terms.html.html theme/_default/t
erms.en.html theme/_default/terms.html theme/indexes/indexes.en.html.html theme/
indexes/indexes.html.html theme/indexes/indexes.en.html theme/indexes/indexes.ht
ml]
INFO 2018/02/20 12:34:36 Alias "\tags\adware\page\1\index.html" translated to "t
ags\adware\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\services\computer\page\1\index.html" translated
 to "services\computer\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\categories\equipment\page\1\index.html" transla
ted to "categories\equipment\page\1\index.html"
WARN 2018/02/20 12:34:36 [en] Unable to locate layout for "taxonomyTerm": [taxon
omy/publication_type.terms.en.html.html taxonomy/publication_type.terms.html.htm
l taxonomy/publication_type.terms.en.html taxonomy/publication_type.terms.html _
default/terms.en.html.html _default/terms.html.html _default/terms.en.html _defa
ult/terms.html indexes/indexes.en.html.html indexes/indexes.html.html indexes/in
dexes.en.html indexes/indexes.html theme/taxonomy/publication_type.terms.en.html
.html theme/taxonomy/publication_type.terms.html.html theme/taxonomy/publication
_type.terms.en.html theme/taxonomy/publication_type.terms.html theme/_default/te
rms.en.html.html theme/_default/terms.html.html theme/_default/terms.en.html the
me/_default/terms.html theme/indexes/indexes.en.html.html theme/indexes/indexes.
html.html theme/indexes/indexes.en.html theme/indexes/indexes.html]
INFO 2018/02/20 12:34:36 Alias "\tags\irs\page\1\index.html" translated to "tags
\irs\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\tags\hack\page\1\index.html" translated to "tag
s\hack\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\services\information\page\1\index.html" transla
ted to "services\information\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\categories\recycle\page\1\index.html" translate
d to "categories\recycle\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\resources\page\1\index.html" translated to "res
ources\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\tags\rootkit\page\1\index.html" translated to "
tags\rootkit\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\tags\ssn\page\1\index.html" translated to "tags
\ssn\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\services\page\1\index.html" translated to "serv
ices\page\1\index.html"
WARN 2018/02/20 12:34:36 [en] Unable to locate layout for "taxonomyTerm": [taxon
omy/tag.terms.en.html.html taxonomy/tag.terms.html.html taxonomy/tag.terms.en.ht
ml taxonomy/tag.terms.html _default/terms.en.html.html _default/terms.html.html
_default/terms.en.html _default/terms.html indexes/indexes.en.html.html indexes/
indexes.html.html indexes/indexes.en.html indexes/indexes.html theme/taxonomy/ta
g.terms.en.html.html theme/taxonomy/tag.terms.html.html theme/taxonomy/tag.terms
.en.html theme/taxonomy/tag.terms.html theme/_default/terms.en.html.html theme/_
default/terms.html.html theme/_default/terms.en.html theme/_default/terms.html t
heme/indexes/indexes.en.html.html theme/indexes/indexes.html.html theme/indexes/
indexes.en.html theme/indexes/indexes.html]
INFO 2018/02/20 12:34:36 Alias "\tags\social-security-number\page\1\index.html"
translated to "tags\social-security-number\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\tags\tax\page\1\index.html" translated to "tags
\tax\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\tags\virus\page\1\index.html" translated to "ta
gs\virus\page\1\index.html"
INFO 2018/02/20 12:34:36 Alias "\tags\w2\page\1\index.html" translated to "tags\
w2\page\1\index.html"

Built site for language en:
0 draft content
0 future content
0 expired content
62 regular pages created
41 other pages created
0 non-page files copied
21 paginator pages created
9 tags created
2 categories created
0 publication_types created
total in 227 ms
WARN 2018/02/20 12:34:36 Skip i18nDir: GetFileAttributesEx c:\Users\trinsic\GitH
ub\scottrlarson\profession\i18n: The system cannot find the file specified.
Watching for changes in c:\Users\trinsic\GitHub\scottrlarson\profession\{data,co
ntent,layouts,static,themes}
WARN 2018/02/20 12:34:36 Skip i18nDir: GetFileAttributesEx c:\Users\trinsic\GitH
ub\scottrlarson\profession\i18n: The system cannot find the file specified.
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableF
astRender
Web Server is available at //localhost:1313/ (bind address 127.0.0.1)
Press Ctrl+C to stop

Im not sure what I am doing wrong at this point, when I reload the page it nothing changes, and I also restarted the server.

2018-02-20%2012_47_25-Computer%20Recycling%20_%20Scott%20Larson%20-%20Pale%20Moon

Are you putting the index call in your content file or a template file? If in your content files, you have to use a shortcode.

That’s a heck of a lot of errors. Can you share your repo please?

that did it, I put the code into a shortcode and called the shortcode from the content page.

Hi Rick, I am customizing the academic template so the errors may be related to the fact that I dont have every thing in the template customized yet: https://github.com/scottrlarson/profession

You can either put it in your single.html and fix its location somewhere, or do it in a shortcode so you can vary the location as needed. Note that, you can also have the single.html check for a setting in your frontmatter, and show or hide based on that.

In a current project, I have some single pages that include a long data table requiring the visitor to scroll, and I want one of those “go to top” jumpers at the bottom after the table. In such a page’s markdown, I put:

 include_pagetop: true

… then in my single above the footer I put:

{{ if isset .Params "include_pagetop" }}
{{ if eq .Params.include_pagetop true }}
{{- partial "pagetop.html" . -}}
{{ end }}
{{ end }}

Then the jumper link appears only when I need it.

Ohh good point on having the template check the front matter for the setting. That might be a better way to do it as I dont want to create a shortcode for each and every varying setting. Thank you.

Note that you can also use with to test. See the docs. It can test for the existence of a frontmatter param, but, it does not care what the param is set to. That’s why I broke that check up into two steps with if isset and if eq. The key to remember is, if you throw a nil from even one page (say you don’t set the frontmatter param on a couple pages), Hugo will throw an error back, unless you do a bit of logic to filter those out.