Using Unmarshal to parse nested JSON

Is there better way to parse json and retrieve values of nested values with unmarshal? I am trying to select only ‘response.docs’ values only.

{{ with resources.GetRemote "https://archive.org/advancedsearch.php?q=collection%3AServantsOfKnowledge+(language%3Aori+OR+language%3A%22Oriya%22)&fl%5B%5D=collection&fl%5B%5D=addeddate&fl%5B%5D=description&fl%5B%5D=identifier&fl%5B%5D=title&fl%5B%5D=description&sort%5B%5D=&sort%5B%5D=&sort%5B%5D=&rows=2&page=1&output=json" }}
  {{ with .Content | unmarshal }}
      {{range $books := .response.docs}}
        {{ warnf "%v" $books}}
      {{ end }}
  {{end}}
{{end}}
1 Like

Not that I can think of.

1 Like

Thank you @jmooring

Slightly refactored the code for readability using the example code provided by @regis in his Hugoconf talk.

 {{ with resources.GetRemote "https://archive.org/advancedsearch.php?q=collection%3AServantsOfKnowledge+(language%3Aori+OR+language%3A%22Oriya%22)&fl%5B%5D=collection&fl%5B%5D=addeddate&fl%5B%5D=description&fl%5B%5D=identifier&fl%5B%5D=title&fl%5B%5D=description&sort%5B%5D=&sort%5B%5D=&sort%5B%5D=&rows=20&page=1&output=json" }}
  {{ with .Content | unmarshal }}
    {{ with .response.docs }}
     {{ warnf "%v" .}}
    {{ end }}
  {{end}}
{{end}}

Note that you can also do:

 {{ with resources.GetRemote "https://archive.org/advancedsearch.php?q=collection%3AServantsOfKnowledge+(language%3Aori+OR+language%3A%22Oriya%22)&fl%5B%5D=collection&fl%5B%5D=addeddate&fl%5B%5D=description&fl%5B%5D=identifier&fl%5B%5D=title&fl%5B%5D=description&sort%5B%5D=&sort%5B%5D=&sort%5B%5D=&rows=20&page=1&output=json" }}
  {{ with . | unmarshal }}
    {{ with .response.docs }}
     {{ warnf "%v" .}}
    {{ end }}
  {{end}}
{{end}}

I haven’t measured what’s the most effective alternative is, though, I suspect they will currently be about equal.

2 Likes

Oh that’s interesting, you can use unmarshal on the resource directly! TILT!

1 Like

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