Muliti level json parsing in Hugo

Hi guys, below is my json.

    [
      {
        "book": [
          {
            "type": "RBOOK",
            "bookCode": "YT",
            "sns": [
              {
                "SN": "1",
                "ays": [
                  {
                    "AYNO": "1",
                    "TXT": "TEST"
                  },
                  {
                    "AYNO": "2",
                    "TXT": "TEST2"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]

and my code is below, it is reading fine but it is not optimised. Appreciate any help to optimise my code to parse the json. With this code I could not build the site in Netlify as there around 7000 pages. Please help I am stuck on this.

    {{ range $json }}
    					{{ range .book }} 
    					{{ range .sns }} 
    							{{ if (eq .SN "1" ) }} 
    								{{ range .ays }} 
    									{{ if and (eq .AYNO "1") }}
    										{{ .TXT | safeHTML }} 
    									{{ end }}	
    								{{ end }}
    							{{ end }}	
    						{{ end }}
    						{{ end }}
    				{{ end }}

Look in the first example and /layouts/shortcodes/lightslider.html

No luck, it gives <$json.book>: can't evaluate field book in type []interface {} error..

my code now is:

{{$json := getJSON $jsonURL }}
{{ range $json.book }}
					{{ $item := . }} 
					{{ range $item.sns }} 
							{{ if (eq .SN "1" ) }} 
								{{ range .ays }} 
									{{ if and (eq .AYNO "1") }}
										{{ .TXT | safeHTML }} 
									{{ end }}	
								{{ end }}
							{{ end }}	
						{{ end }}
						 
				{{ end }}

will $json get anything?

start with simple templates

{{$json := getJSON $jsonURL }}
 elements: {{  len $json.book }}
{{ range $json.book }}
     {{ .type | safeHTML }} 
{{ end }}

something in this way

1 Like

Hi,

What exactly are you trying to accomplish? What kind of output are you looking for?

Thank you for the support. Finally it got worked. I have changed json structure a bit. and the code worked well as in the data template example.

  {
    "book": 
      {
        "type": "RBOOK",
        "bookCode": "YT",
        "sns": [
          {
            "SN": "1",
            "ays": [
              {
                "AYNO": "1",
                "TXT": "TEST"
              },
              {
                "AYNO": "2",
                "TXT": "TEST2"
              }
            ]
          }
        ]
      }
  }