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

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