XML to JSON in Golang Demystified

Keiran Smith
Level Up Coding
Published in
3 min readJan 22, 2020

--

There comes a point in every developer's life where XML data is painfully unavoidable to work with. RSS Feeds try to make this easier but everyone prefers good ol’ JSON, unless you are a sadist.

In Golang we can attack this in a number of ways like using an XML to JSON Library, but why not use the Go standard libraries and save some vendoring issues.

Building our own XML to JSON function

I was recently working on something that required a Medium RSS feed published to a website and thought why not use a GoLang function running on OpenFaaS to accomplish this.

Create a new function

To create our new function we need to use the OpenFaaS CLI

$ faas new medium2json --lang=go

Now we have a super simple Golang handler for OpenFaaS.

Import our Libraries

There are a few libraries required by our function. So let's import them. Open function/handler.go and add the imports under package

You will notice I added net/http so let's make use of it.

GETing our RSS Feed

Regular Medium users will know that an RSS feed is exposed for each user at https://medium.com/feed/@affix

We can utilize this feed to get what we need using a simple HTTP GET with the net/http package in Go. Let's add it to our Handle function:

The above code is pretty simple, but I always get asked about the defer statement.

What exactly can happen if you don’t defer?

The answer is pretty simple. Without defering the Close(), you create a resource leak. While the…

--

--

Husband 🤴🏻Hacker 💻 Developer 🤓 Scot 🏴󠁧󠁢󠁳󠁣󠁴󠁿 OSCP , OSEP, OSWE, OSED, OSCE3 Certified 🤩 golang 🤩 kubernetes 🤩 cloud 🤩 python 🤩 Rauby