Ranging over Google Review JSON

Hi,
I have a Hugo variable that has been populated by get.JSON for google reviews.
$reviews := getJSON “https://maps.googleapis.com/maps/api/place/details/json?fields=name%2Crating%2Creviews&place_id={{PLACE_IS}}&key={{API_KEY}}”}}.


The data is a dense JSON with maps that I am having trouble extracting into usable on-page content.
I have tried various versions of Range and With. But I am trying to extract the Rating, author_name and text for on-page display
I can get the data shown in the image using range or with combinations e.g.

{{ range $index, $reviews := $reviews }}
{{ . }}
{{end}}

Can anyone help with parsing out just the three items I am interested in displaying?
Thanks.

When I am wrangling JSON data, I find it helpful to view it in a more structured format. Something like:

{{ $placeDetails := getJSON "https://maps.googleapis.com/maps/api/..." }}
<pre>{{ jsonify (dict "indent" "  ") $placeDetails }}</pre>
Example
{
  "name": "Patty's Eggnest",
  "rating": 4.2,
  "reviews": [
    {
      "author_name": "James Tamietti",
      "author_url": "https://www.google.com/maps/contrib/102205583676073090241/reviews",
      "language": "en",
      "profile_photo_url": "https://lh3.googleusercontent.com/a-/AFdZucoT9-_GBdIPDwdsT09Qihk5klb0Ttt1uw_njtK4=s128-c0x00000000-cc-rp-mo",
      "rating": 2,
      "relative_time_description": "in the last week",
      "text": "I’ve been driving by Patty’s Eggnests for a couple years. Always meant to try the place, but somehow never got to. Today, I fixed that and found it pretty disappointing.\n\nIt all started from the service. I was helped to a table quickly and my coffee was there in a quick way. Then they forgot about me and didn’t come back for 10 minutes. I wouldn’t care that much if the place was busy, but it really wasn’t. Once the order was taken, the food came quickly. Then once again they forgot about me because they never once came back to refill the coffee.\n\nAll this would be forgivable if the food was great, but it really isn’t. The portion size was small. Most places would give you four pieces of toast. This place gives you two (it is slightly thicker than your average Denny’s toast). The hash browns are just a thin rectangle slab, nothing more, nothing less. The omelette was supposed to be four eggs, but they must have been tiny eggs.\n\nI want to add that the food isn’t that bad. Just not that great for a place that specializes in breakfast.\n\nI’m not actively trying to discourage people to eat here, but I’m also not encouraging it either. Between the hit and miss service and mediocre food, I won’t go out of my way to eat here again.",
      "time": 1657585512
    },
    {
      "author_name": "Jax Rose",
      "author_url": "https://www.google.com/maps/contrib/116024635476306649623/reviews",
      "language": "en",
      "profile_photo_url": "https://lh3.googleusercontent.com/a/AItbvmlaZXddR8-6J_1lLk0OQObTaFovTyJOAOWBGOHJ=s128-c0x00000000-cc-rp-mo",
      "rating": 5,
      "relative_time_description": "in the last week",
      "text": "They were slammed, and I still got a delicious breakfast and friendly service in a matter of minutes. Very well-run, with experienced management and staff. Traditional and inventive menu choices. So good!",
      "time": 1657504547
    },
    ...
  ]
}

To display the place details, do something like:

{{ with $placeDetails }}
  <h2>Reviews of {{ .name }} (rating = {{ .rating }})</h2>
  {{ range .reviews }}
    Author name: {{ .author_name }}<br>
    Text: {{ replace .text "\n" "<br>" | safeHTML }}
  {{ end }}
{{ end }}

Here’s an example that I created a while back:

git clone --single-branch -b hugo-forum-topic-32881 https://github.com/jmooring/hugo-testing hugo-forum-topic-32881
cd hugo-forum-topic-32881
hugo server

But you’ll need to change the API key on line 6 of layouts/_default/single.html. Example output:

9 Likes

Hi, This looks like a very elegant solution.
You also appear to have provided me with a snap-in solution.
I am looking forward to trying this tonight after work.

Many thanks again.

Thanks again jmooring.
I got some pretty good looking google comments on the site now using your code.


Many thanks again.
Charles.

1 Like

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