Trouble parsing "OPML" JSON file

I have converted an OPML file to JSON in data/blogroll.json that I want to parse. I created a layout layouts/blogroll.html that parses the file, but I keep getting errors:

Error: Error building site: failed to render pages: render of "page" failed: "/Users/REDACTED/Code/REDACTED/themes/smpl/layouts/_default/blogroll.html:12:20": execute of template failed: template: _default/blogroll.html:12:20: executing "_default/blogroll.html" at <index $item "_htmlUrl">: error calling index: cannot index slice/array with type string

This is my setup, can someone tell me how to get a working template?

blogroll.html

...
<ul>
  {{ $blogroll := $.Site.Data.blogroll.body }}
  {{ $groups := $blogroll.outline }}
  {{ range $groups }}
  <li>
    {{ ._text }}
    <ul>
      {{ $items := .outline }}
      {{ range $items }}
      {{ $item := . }}
      <li>
        <a href="{{ index $item "_htmlUrl" }}">
          {{ index $item "_text" }}
        </a>
      </li>
      {{ end }}
    </ul>
  </li>
  {{ end }}
</ul>
...

blogroll.json

{
	"head": {
		"title": "REDACTED subscriptions in feedly Cloud"
	},
	"body": {
		"outline": [
			{
				"outline": [
					{
						"_type": "rss",
						"_text": "Henrique Dias",
						"_title": "Henrique Dias",
						"_xmlUrl": "https://hacdias.com/all/feed.xml",
						"_htmlUrl": "https://hacdias.com/all/"
					},
					{
						"_type": "rss",
						"_text": "Horst Gutmann: zerokspot.com",
						"_title": "Horst Gutmann: zerokspot.com",
						"_xmlUrl": "https://zerokspot.com/index.xml",
						"_htmlUrl": "https://zerokspot.com/"
					}
				],
				"_text": "People",
				"_title": "People"
			},
			{
				"outline": [
					{
						"_type": "rss",
						"_text": "Bits of Freedom",
						"_title": "Bits of Freedom",
						"_xmlUrl": "https://www.bitsoffreedom.nl/feed/",
						"_htmlUrl": "https://www.bitsoffreedom.nl"
					},
					{
						"_type": "rss",
						"_text": "Cryptomator Blog",
						"_title": "Cryptomator Blog",
						"_xmlUrl": "https://cryptomator.org/feed.xml",
						"_htmlUrl": "https://cryptomator.org/"
					}
				],
				"_text": "Privacy",
				"_title": "Privacy"
			}
		]
	},
	"_version": "1.0"
}

I don’t get an error when I try to replicate this. Do you have your site code in a repo somewhere we can look at so we can test?

1 Like

It’s not in a repo yet. I’ll create one for testing purposes.

This is awkward…

I created this repo:

and when I run this locally, it’s working…

you use the index function for a string variable. like the error says.

$item is no array !

try $item._htmlUrl

I’m a small step further in my quest. The original OPML file is much bigger. I copied the demo json file and now it’s working. So it must have to do with the contents or the size of the original file. I’ll investigate further and keep you guys updated.

Thanks so far.

No. It’s a map. And index works for collections, maps and arrays. I can see that when I output <li>{{ $item }}</li>, which is working. Since the demo does work, the index is just fine. But I’ll give your alternative a try.

I had the assumption that the file would have a fixed structure: a category with a list/map of entries.

Diving deeper, I noticed 2 differences:

  • empty categories
  • categories with a single item

The latter caused the issue. In pseudo code they look like:

{ "category": 
  { "title": "the title", "url" : "https://example.org/" } 
}

where a map would look like

{ "category": [ 
  { "title 1": "the title", "url 1" : "https://example.org/" },
  { "title 2": "the title", "url 2" : "https://example.com/" }
] }

I guess I’ll have to test for this and fix the code accordingly.