Tips and tricks for manipulating data objects/maps/arrays

Hi Everyone,

I’ve been working on my first Go and Hugo project recently and am really enjoying it. Thank you to all of those who have clearly put in a great deal of work.

I’m trying to get to grips with templating in Hugo and with data manipulation whether simply from files in the data directory of with getJSON.

I would be really grateful if anyone is able to give some tips/tricks and examples of how best to get at and use data at the various different levels of a json (or equivalent) structure. I’m managing to get at certain elements but clearly need to build my knowledge and understanding further! :slight_smile:

For example, here is a data file I’m working with called robots.json

  [
  {
    "robot_type" : "one",
    "models" :  [
      {
        "id": 175676783,
        "Name": "Triton",
        "ALink": "https://bbc.co.uk/news"
      },
      {
        "id": 27635689,
        "Name": "Mars",
        "ALink": "https://bbc.co.uk/sport",
        "otherNames" : ["red rover", "planetary explorer", "red duster"]
      },
      {
        "id": 334756,
        "Name": "Active 2i",
        "ALink": "https://bbc.co.uk/weather"
      },
      {
        "id": 4895709,
        "Name": "europa",
        "ALink": "https://bbc.co.uk/"
      }
    ]
  },
  {
    "robot_type" : "two",
    "models" :  [
      {
        "id": 124778,
        "Name": "Mars",
        "ALink": "https://bbc.co.uk/contact"
      },
      {
        "id": 876523,
        "Name": "Active 2i",
        "ALink": "https://bbc.co.uk/terms"
      }
    ]
  }
]

What I want to be able to do is to loop through the various models and extract any and all data either using a loop or separately getting a specific pice of information for a particular robot.

I’m making use of range currently but have struggles to access beyond the first layer. I’m very new to Hugo and go and would be able to do this in js so I’m sure I’ll get there but any help along the way would be great!

If I can help to explain what I’m trying to do further, do just let me know.

Thanks again!

Well, one way is to keep ranging through things, so if your data file is called data

{{$data := .Site.Data.data}}
  {{range $data}}
    {{range .models}}
      {{.Name }}<br />
    {{end}}
  {{end}}

would return:

Triton
Mars
Active 2i
europa
Mars
Active 2i

Or, to get at otherNames you’d want to wrap it in a conditional (with is nice for this)

{{range $data}}
    {{range .models}}
      {{with .otherNames}}
        {{range .}}
          {{.}}<br />
        {{end}}
      {{end}}
    {{end}}
  {{end}}

Hopefully that’s a start to help you start stepping through it. If you just define the data {{$data := .Site.Data.data}} you can print it to the page and see the structure that’s there: {{$data}}

2 Likes

Hi Bud,

Thank you very much indeed for your help. Hopefully some others might find this thread helpful too - getting used to data manipulation with Hugo has taken me a bit of playing!

A thought I have is that it would be useful to be able to work with data models which are of relatively unknown construct. Are there any Hugo samples of recursive functions and/or for…in loops etc. that could help to handle unknown data structures?

The forums here are pretty rich in ideas.

1 Like

Yes - I’ll dig deeper :slight_smile:

…Edit… range and seq appear to be very helpful!