Use getJSON in archetype file to display weather information

hi there,

i was reading this discourse when i stumbled upon a rather inconspicuous answer by @bep where he says you can use getJSON in Archetypes so i came up with the idea to display the weather that has been present while writing a blog post - i know i could just write it down every time, but why not automate it if its possible?

My approach currently works quite fine but to me it feels as if there was a better way to do this. Currently i use getJSON like this in my archetypes/ file:

title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
author: ".."
cover: "imgs/canyon2-font.jpg"  
tags: ["tag1", "tag2"]
temp: {{ $dataJ := getJSON ""  }}
{{ range first 1 $dataJ.list }}
    {{ $tempCelcius :=  sub .main.temp 273.15 }}
    {{ $tempCelciusRounded := math.Round $tempCelcius}}
    {{ $tempCelciusRounded }} 
{{ end }}

Edit: i altered the getJSON URL

this works but say i want to use some more information i need to do that over and over again. Is there a way to use something like shortcodes in archetypes? I just cannot find where and how i.e. {{ .Date }} is implemented.

here is how it looks currently:

With my understandig - this does not work.

Build a script around “hugo new” and load the JSON file to /assets

You can use a day paramater to point to the file like

day: {{ now.Format "2006-01-02T15:04:05Z"}}

The build your templates around it.

That won’t work. A quick idea would be the following: You write a little bash script (or whatever scripting is available in your environment) that does the following:

  • create the new hugo post (do it as page bundle, foldername = title, then inside
  • load the current weather from openweatherapi, put it into the page folder like weather.json
  • in your template for single posts load the data file and go on as wished

something like this. The way I understand the openweatherapi you won’t be able to check by date, so this will be a one-time thing on post creation.

Thank you guys for your fast reply! I had already expected those answers to come as a solution.
Just to not get future readers confused: Using getJSON in archetype works as mentioned above, its just not a very clean solution.

I still wonder where {{ .Date }} is defined and if I could achieve something similar.

Hmm, not sure if I got your question completely wrong.

Is it: I want to load a json file when creating the file and add all parameters that are in the json file into front matter? If so, I am asking WHY :wink: because then they are confined within the frontmatter. and in about two months you ask yourself how to create some kind of archive based on the temperature on the day of posting. My advice is to use data files for that. You can iterate easily over them and will have an archive of older dates.

If you just want to put everything into frontmatter and don’t care about this all it should be possible to iterate over the json file using range. The problem here is that you don’t know how “deep” the file goes. It’s not a one level response, there will be weather previews for the next 7 days in some kind of a subarray … You either manually check for each expected parameter or you have to write some kind of check if the current “node” is an array/object or a string.

sure in the end you have one big frontmatter part with all the date, but you will be much more flexible if you leave the response in its current format in a data file (either in the folder or up some levels in the data folder) and then reuse it.

That’s just my opinion :wink:

Regarding the templating of the archetype: that’s new to me. I wonder if there are any hidden danger spots with new lines.

Regarding the definition of .Date I recently read a thread here that tried to use .Date.Local in the archetype which is not possible (at this time). .Date is just a string of the current date. You can use dateFormat though to do some stuff with it.

sorry for confusion! I guess it’s not a very common use case.
Yes, I want to load the current weather data when using hugo new posts/ My idea was something like “what was the weather like when i wrote this post…
This works as shown above, but i agree storing the data for later use might be a good idea.

The front matter matching the example above currently looks like this after using hugo new … :

title: "Example Post Displaying Weather Data"
date: 2019-08-19T10:33:33+02:00
draft: true
author: "me"
cover: "imgs/canyon2-font.jpg" 
tags: ["tag1", "tag2"]
temp: 23 
icon: 01d
weatherDescription:  clear sky

which I obviously can access using i.e. {{ .Params.temp }} in a layout file which is quite handy.

Hmmyeah, but how about a shortcode that takes the weather.json from your current folder and gives you access to all the fields in that file :slight_smile: {{% weather icon %}}

If in a year from now you want to include the wind direction you can easily adapt from the data-file version, but not the frontmatter version.

My own blog is 15 years old and I won’t be able to retrieve weather data from back then :wink: that’s where i am coming from.

yeah sure. but the way i tried it was perfect for my use case so i thought why not make it even easier. nevertheless thank you for your support! :slight_smile: