YAML vs JSON vs XML in Go.

Andrew Hayes
Geek Culture
Published in
6 min readJun 15, 2021

--

In a recent Go project, I had to choose what data format to use for sending and receiving data. My usual go-to is JSON, but I decided I would give an honest comparison of the options. As I’m sure you’ve gleaned from the title, the main contenders are YAML, JSON and XML. The criteria I have evaluated them on are:

  1. Human readability. It’s good to be able to read the data during development and debugging.
  2. Size. What size will the data be once it’s been encoded?
  3. Speed. How long does it take to parse and encode the formats?

To evaluate them I needed a decent amount of data, that’s fairly complex and is freely available (so I can write about it here without worrying). The data I have chosen is the Debian Vulnerability Data, available in JSON here. It’s freely available, has a few thousand entries and has multiple layers, so it’s perfect! (It’s updated daily, so might have changed since I took a copy). I modified it slightly to play better with the chosen formats (looking at you XML!). I then wrote a small Go program to load the data in, Unmarshal it using each format and then Marshal them using each format. The full code and data can be found on Github. A small sample looks like this:

func unmarshalThenMarshalYAML(dataBytes []byte) (int64, int64) {  vulnData := DebianVulnData{}

--

--