DEV Community

petercour
petercour

Posted on

Parsing JSON in GO

JSON is everywhere. It's used by many websites to communicate data through APIs. If you send a request to an API, it's often either JSON format or XML format (yuck!)

JSON is nicely formatted data using brackets and quotes. It's easier for a computer to parse.. or is it?

It's hard to parse JSON in Go. Well, for a novice that is. To make it easier, you can use structs. But if you have complex data, it's very cumbersome to write a struct.

Isn't there an easier way? Yes, there is.

So there's a tool you can use to convert JSON to Golang structs: https://mholt.github.io/json-to-go/

On the left paste your JSON. On the right a Golang struct wil be created. Copy that struct into your golang code.

Then use the module "encoding/json" to parse the JSON data, like so https://golangr.com/json/

And your set!

Top comments (0)