How to replace the data.GetJSON with resources.GetRemote or resources.Get

Hi there,

Previously I was using google place Api to fetch and display reviews but now they do not fetch new reviews dont know why. I am getting this ERROR deprecated: data.GetJSON was deprecated in Hugo v0.123.0 and will be removed in Hugo 0.138.0. use resources.Get or resources.GetRemote with transform.Unmarshal.

The funny things is I do not use data.GetJSON any where the only place I use getJSON is in the below partial.

{{ if hugo.IsProduction }}
{{ $googleReviews := .Site.Params.widget.reviews.google }}
{{ $apikey := $googleReviews.api_key }}
{{ $place_id := $googleReviews.place_id }}
{{ $url := printf "https://maps.googleapis.com/maps/api/place/details/json placeid=%s&key=%s" $place_id $apikey }}
{{ $data := getJSON $url }}
{{ with index $data "result" }}

How can I fix this now? it makes no sense I try to replace getJSON but it fails.

Thanks

First, getJSON is an alias of data.GetJSON as described in the relevant documentation:
https://gohugo.io/functions/data/getjson

Second, because you are accessing a remote resource, you need to use the resources.GetRemote function instead of data.GetJSON. The first example in the Remote data section shows you exactly how to do that.

1 Like

Thanks @jmooring

I am lost in the documentation which is showing me to do exactly as I have currently done it using getJSON, but the Hugo build is failing.

So based on this what I need to change in below code as my understanding is that the code is exactly as Hugo wants it to be or I am missing the point here.

{{ if hugo.IsProduction }}
{{ $googleReviews := .Site.Params.widget.reviews.google }}
{{ $apikey := $googleReviews.api_key }}
{{ $place_id := $googleReviews.place_id }}
{{ $url := printf "https://maps.googleapis.com/maps/api/place/details/json placeid=%s&key=%s" $place_id $apikey }}
{{ $data := getJSON $url }}
{{ with index $data "result" }}

Thanks

That one.

{{ $googleReviews := .Site.Params.widget.reviews.google }}
{{ $apikey := $googleReviews.api_key }}
{{ $place_id := $googleReviews.place_id }}
{{ $url := printf "https://maps.googleapis.com/maps/api/place/details/json placeid=%s&key=%s" $place_id $apikey }}

{{ $data := dict }}
{{ with resources.GetRemote $url }}
  {{ with .Err }}
    {{ errorf "%s" . }}
  {{ else }}
    {{ $data = . | transform.Unmarshal }}
  {{ end }}
{{ else }}
  {{ errorf "Unable to get remote resource %q" $url }}
{{ end }}

{{ with index $data "result" }}
  {{ . }}
{{ end }}

The code above is verbose compared to your earlier code due to error checking, a key aspect of defensive coding.

I haven’t tested the above because I don’t know the values of:

  • $googleReviews
  • $apikey
  • $place_id

And this URL in your code doesn’t look right. It has a space:

https://maps.googleapis.com/maps/api/place/details/json placeid=%s&key=%s
1 Like

Thanks again @jmooring

It now make sense but your example is way complicated, I instead made a minor change and it works.

{{ if hugo.IsProduction }}
{{ $googleReviews := .Site.Params.widget.reviews.google }}
{{ $apikey := $googleReviews.api_key }}
{{ $place_id := $googleReviews.place_id }}
{{ $url := printf "https://maps.googleapis.com/maps/api/place/details/json placeid=%s&key=%s" $place_id $apikey }}
# changed old code {{ $data := getJSON $url }} to
{{ $data := resources.GetRemote $url }}
{{ $data = $data | transform.Unmarshal }}
# end new code
{{ with index $data "result" }}

Thanks again for quick and on point support

OK, what happens when the server doesn’t respond? Or when the api key expires?

Error checking is really, really important. Coding is a garbage-in garbage-out exercise. I suggest you reconsider your choice and take some time to understand the “way complicated” example.

There’s a reason the documented example includes error checking.

6 Likes

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