Use JSON value in Content

I have a data/ directory in the same level as the content/ directory. Inside the data/ directory, there is a JSON file which has:

{
  "accounts": [
    {
      "name": "Stack Overflow",
      "url": "https://stackoverflow.com/users/13055097/saurabh"
    },
    {
      "name": "LinkedIn",
      "url": "https://linkedin.com/in/saurabhsm"
    },
    {
      "name": "GitHub",
      "url": "https://github.com/saurasm"
    }
  ]
}

How do I use the Github url from the above JSON file in the content (markdown file) ?

On line 2 here, I’m linking “on GitHub” with “GitHub - saurasm/mockroblog: Reddit backend re-visit”, but would like to do “.Site.Data.socialmedia.accounts.url[2]/mockroblog” or something similar.

To obtain the value:

(index (where site.Data.socialmedia.accounts "name" "GitHub") 0).url

I don’t know what this means.

If you want to insert the value into your content, create a shortcode.

“on GitHub” text here contains a link in https:// format here

I’m looking to use the URL from here and append it to a string, without having to specify it in https:// format.

See my previous reply.

I’ve created a shortcode projecturl.html with:

{{ $github_profile := (index (where site.Data.socialmedia.accounts "name" "GitHub") 0).url }}
{{ $project_base := .Get 0 }}
<a href="$github_profile$project_base"></a>

And inside the file I’m using [on GitHub][2], where [2] is {{< projecturl "/mockroblog" >}}. The text gets highlighted, but the link doesn’t work.

A markdown link has three components: link text, a link destination, and optionally a link title.

[Post 1](/posts/post-1 "My first post")
 ------  -------------  -------------
  text    destination       title

The link destination is a URL, not an anchor element.

I have managed to get it working without using the optional “title” field with the below shortcode:

{{ $github_profile := (index (where site.Data.socialmedia.accounts "name" "GitHub") 0).url }}
{{ $text := .Get 0 }}
{{ $destination := .Get 1 }}
{{ $path := print $github_profile $destination }}
<a href="{{ $path }}" target="_blank" rel="nofollow noopener noreferrer">{{ $text }}</a>

and using it in the content like:

{{< projecturl "on GitHub" "/mockroblog" >}}

Thanks @jmooring !

You are using it in content, not front matter. Front matter is the JSON/TOML/YAML at the top of the markdown file.

1 Like

Sorry about that!! I’ve updated the post.

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