Count items via json file

I have a shortcode to count some cities in the world base on a json file as follows:

<ul>
	{{ range .Site.Data.places }}
	{{ $p := . }}
		<li>{{ $p.properties.name }} (<strong>{{ $p.properties.country }}</strong>) </li>
	{{ end }}
</ul>

{{ $json := getJSON "data/places.json" }}
<strong>{{- len $json -}}</strong> cities in <strong> ?? countries </strong>

The json file is as follows :

[
	{
	"type": "Feature",
	"properties": {
	"name": "New York City",
	"country": "USA",
	"description": null
	},
	"geometry": {
	"type": "Point",
	"coordinates": [-74.0059728, 40.7127753]
	}
	},
	{
	"type": "Feature",
	"properties": {
	"name": "San Fransisco",
	"country": "USA",
	"description": null
	},
	"geometry": {
	"type": "Point",
	"coordinates": [-122.443, 37.7562]
	}
	},
	{
	"type": "Feature",
	"properties": {
	"name": "Mexico City",
	"country": "Mexico",
	"description": null
	},
	"geometry": {
	"type": "Point",
	"coordinates": [-99.1333, 19.4333]
	}
	}
]

I am able to count the number of cities (with len $json) but I have difficulties to count the associated countries (without duplication)
Thanks in advance for your help

I found this on the web. Maybe you can exploit it for your purpose.

{{ $countries := slice }}
{{ range site.Data.places }}
  {{ $countries = $countries | append .properties.country }}
{{ end }}
{{ $numberOfUniqueCountries := $countries | uniq | len }}

I can’t achieve displaying the number of countries with your proposed code.
Nothing is displayed.
Still investigating how to do it

The code doesn’t display a value. It sets the value of $numberOfUniqueCountries.

To display the value:

{{ $numberOfUniqueCountries }}

Perfect !
It works.
Thanks a lot

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