# Range last n entries of JSON data file

**URL:** https://discourse.gohugo.io/t/range-last-n-entries-of-json-data-file/8211
**Category:** support
**Created:** [August 31, 2017, 2:47pm UTC](https://discourse.gohugo.io/t/range-last-n-entries-of-json-data-file/8211 "2017-08-31T14:47:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mxmehl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mxmehl/32/4039_2.png) [@mxmehl](https://discourse.gohugo.io/u/mxmehl)
#### Post date: [August 31, 2017, 2:47pm UTC](https://discourse.gohugo.io/t/range-last-n-entries-of-json-data-file/8211/1 "2017-08-31T14:47:23Z")

</div>

I have following code to show all entries in a JSON data file (/data/signatures/signatures.json):

```auto
{{ range .Site.Data.signatures }}
  {{ range sort . "id" "desc" }}
     <tr>
        <td>{{ .name }}</td>
     </tr>
  {{ end }}
{{ end }}

```

How can I only show the **last** n entries in this data file?

I tried `last 10 .Site.Data.signatures` but got `can't iterate over map[string]interface {}`. Thank you for your help!

The data files looks like this:

```auto
[
  {
    "name": "Oldest",
    "id": 0
  },
  {
    "name": "Second",
    "id": 1
  },
  {
    "name": "Newest",
    "id": 2
  }
]

```

---

<div class="post-metadata">

### Author: ![rdwatters](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rdwatters/32/2848_2.png) [@rdwatters](https://discourse.gohugo.io/u/rdwatters)
#### Post date: [August 31, 2017, 4:02pm UTC](https://discourse.gohugo.io/t/range-last-n-entries-of-json-data-file/8211/2 "2017-08-31T16:02:04Z")

</div>

Look here:

> **[last](https://gohugo.io/functions/last)**
>
> slices an array to only the last Nth elements.

---

<div class="post-metadata">

### Author: ![mxmehl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mxmehl/32/4039_2.png) [@mxmehl](https://discourse.gohugo.io/u/mxmehl)
#### Post date: [August 31, 2017, 6:30pm UTC](https://discourse.gohugo.io/t/range-last-n-entries-of-json-data-file/8211/3 "2017-08-31T18:30:45Z")

</div>

Could you please help with my specific example? When doing `{{ range last 10 .Site.Data.signatures }}`, I get

`error calling last: can't iterate over map[string]interface {}`

Is it because I use a JSON file?

---

<div class="post-metadata">

### Author: ![mxmehl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mxmehl/32/4039_2.png) [@mxmehl](https://discourse.gohugo.io/u/mxmehl)
#### Post date: [September 5, 2017, 9:14am UTC](https://discourse.gohugo.io/t/range-last-n-entries-of-json-data-file/8211/4 "2017-09-05T09:14:35Z")

</div>

Please allow me to push this issue. I’m sure the solution isn’t hard…

---

<div class="post-metadata">

### Author: ![mxmehl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mxmehl/32/4039_2.png) [@mxmehl](https://discourse.gohugo.io/u/mxmehl)
#### Post date: [September 5, 2017, 3:00pm UTC](https://discourse.gohugo.io/t/range-last-n-entries-of-json-data-file/8211/5 "2017-09-05T15:00:51Z")

</div>

Thanks to a helping hand, the issue is solved now. Since my constellation is a bit more complicated (additional if condition) I’ll post my current code which is different from the snippet I posted in the first post.

The main lessons I learnt:

- Loading a JSON file into a variable makes things easier. By this I can already apply conditions (display only those with “permissionPub” = “yes”)
- `first` and `last` can be easily applied as function to a `range sort` command. In the first post this would already have been possible in line 2 but then the condition wasn’t working as I wanted

```auto
{{ $json := getJSON "data/signatures/signatures.json" }}
{{ $json := where $json "permissionPub" "yes" }}

{{ range sort $json "timestamp" "desc" | first 10 }}
   <tr>
      <td>{{ .name }}</td>
   </tr>
{{ end }}

```
