JSON error in pulling in simple key:value pairs

I’m trying to replace some static values in content files with data from a curated JSON document, via shortcodes.

Data file: design-tokens/tokens.json

{
  "COLOR_TEXT_LINK": "#098ae4",
  "COLOR_TEXT_LINK_VISITED": "#098ae4",
  "COLOR_TEXT_LINK_HOVER": "#004d81",
  "COLOR_TEXT_LINK_ACTIVE": "#098ae4",
  "COLOR_TEXT_LINK_DISABLED": "#333333"
}

Content file: buttons.md

{{% datapoint token="COLOR_TEXT_LINK" %}}

Shortcode file: datapoint.html

{{ $token := .Get "token" }}
{{ $data := getJSON "./design-tokens/tokens.json" }}
{{ range $data }}
{{ $token }}
{{ end }}

Produces this error:

Building sites … ERROR 2018/05/23 16:57:11 Cannot read json from resource ./design-tokens/tokens.json with error message unexpected end of JSON input

The idea here is that I’m maintaining my list of tokens in one file and they will always get replaced in the content with updated hex codes. The above may be overkill, or just incorrect syntax all around. After researching and trying many things, I’m resorting to asking here!

I would suggest you try the JSON in an online JSON validator.

That said, I would recommend you put these types of JSON files into /data,

If /data/design_tokens/tokens.json

You can access it with .Site.Data.design_tokens.tokens (note me not using a hyphen, which would have made the lookup more complex).

Also, I don’t understand your “range syntax”, but that is a problem for the future, I guess.

1 Like

Yep, that definitely makes things simpler, so I ended up using this bit in my shortcode template:

{{ $token := .Get "token" }}
{{ $data := .Site.Data.design_tokens.tokens }}
{{ index $data $token }}

Thanks!

1 Like

A post was split to a new topic: Help with getJSON: unexpected end of JSON input