Count number of data entries

I have a JSON data file called /data/signees/signees.json which constantly changes. How can I count the number of entries in this JSON file?

Ideally, I want to insert the number in a text, too. My idea was to do that with replace:

{{ replace .Site.Params.text "$count" "123" | markdownify }}

The “123” should be replaced by the actual counted number.

Additional question if I may: How can I replace two strings with such data-driven numbers?


To get the number of entries in a variable, you can use the len function. Something like {{ $data | len }}.

I’m not quite sure what you’re asking about with the replacing strings thing… if signees.json has something like

  "foo": "bar baz qux"

and you want to replace “baz”, you could use replace (or replaceRE if you need something a little more flexible). Something like {{ replace $ " baz " $whatever }}.

1 Like

Thanks for the reply. len was the missing hint!

Now I count the number of JSON entries with

{{ $json := getJSON "data/signees/signees.json" }}
{{- len $json -}}

To replace two strings in a string, you can nest multiple replace functions:

{{ (replace (replace .Site.Params.action.description "$ORGS" (partial "functions/count_organisations.html" .) ) "$INDS" (partial "functions/count_signatures.html" .))  | markdownify }}