Hugo

getJSON url response reformat before use

Hi,

I am trying to use the native

getJSON $url

to retrieve my data but i want to change the response structure that is being sent back. (the api call isn’t mine so can’t alter in server)
how can i do that? Or do i just have to write actually javascript to do it?
was tryin to leverage my already written templates.

Thanks,

Do you mean the response that is coming to hugo from $url or, what if anything is being sent from hugo to $url.

I have the following line of code

  {{ $data := getJSON "https://api.meetup.com/2/events?&sign=true&photo-host=public&group_urlname=techqueria&status=past&text_format=plain&page=20" }}

$data contains an array of objects and I want to change the structure that it has now so that it works with my current implementation of templates.

so lets say that the returned type returns a field called name, I want to change that to title and mutate other fields in the object as well

Hmm, yeah, if it were me, I would pull that json with a script, manipulate it with jq to what you need, then pull it into your templates by doing getJSON on the local file.

https://stedolan.github.io/jq/

2 Likes

yea i gess ill just create another template. Just wanted to see if there was like some easy way to manipulate the data that would make it easy to re-use the same template even though the fields don’t match

Like @RickCogley mentioned, jq is great for this (I’m a recent fan).

Also, if you give us more info of what you’d like your JSON structure to look like, maybe we can help you do this in your template.