Parsing JSON file with Go

 
 

In this tutorial we will learn about parsing JSON file with Go, nothing to worry about as we will try to keep this very simple, basic JSON parsing with Go we could say but we will provide enough information to familiarise yourself with Go structs, functions and a simple way of reading JSON files content. This example will contain two files, a JSON file called appinfo.json and a Golang file as well called main.go, file that will contain our application code. Many of you may find Golang confusing or maybe you are just starting now to code with Go which is not a bad idea at all, we can say that Golang is a fascinating programming language, precise, simple, easy, fast and most important very focused. Choosing Golang as your main programming language is definitely not a bad idea and can be used not only for simple tasks like parsing JSON files but for large scale applications.

Table of contents

JSON file content
Go application dependencies
Structs definition
Main function
JSON parsing function
Test JSON parsing

JSON file content

As we said in the intro of our tutorial our example application will contain only two files, appinfo.json and main.go as well. Below we have the content of our first file called appinfo.json where we have stored some basic string values in a JSON format. Using your preferred text editor for JSON files please add the next lines listed below and save the content into a file name called appinfo.json, this name will be later used within our Go application code:

{
    "application" : [

        {
            "developer": "John Doe Inc",
            "appname": "Parsing JSON File with Golang",
            "version" : "0.0.1",
            "releasedate" : "05/20/2019"
        }
    ]
}

Go application dependencies

Dependencies? Yes, our application will need some external modules in order to read JSON encoded files and also to format our output. If you are not familiar with Go at all then let’s start this step by creating a file named main.go, this is the file that will contain our application code. Once the file is created then lets open it and add the next lines:

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
)

The above lines within our application basically instructs Go that this file is our main application file and also tells Go that we would like to import three external modules, encoding/json, fmt and io/ioutil, modules that will be used later in our tutorial.

Structs definition

When using Go for parsing JSON files we could have two methods depending on the JSON file content structure, first method is commonly known as structured data and the second method is called obviously unstructured data. In this tutorial we will be using structured data method as we know exactly the data structure within our JSON file, for this we will use Golang’s structs. For unstructured data we would use maps but we will talk about this topic in a different tutorial, for now lets add the next lines within our main.go file as well:

type ApplicationNodes struct {
	ApplicationNodes []ApplicationData `json:"application"`
}

type ApplicationData struct {
	Developer   string `json:"developer"`
	AppName     string `json:"appname"`
	Version     string `json:"version"`
	ReleaseDate string `json:"releasedate"`
}

If you are now checking the content of appinfo.json file you can see how struct works, basically we are using struct to define in a structured, precise way what data we need to pull from our JSON file. Be aware that if you don’t define your struct values using CamelCase style these won’t work, can’t be used, exactly as we said before Go is a precise programming language.

Main function

As you can see not too much happens within our main() function, our JSON parsing code will be actually present in a different function listed below but for educational purpose we will call the second function that handles JSON parsing code right from our main() one, is more like an exercise if you want just to familiarise yourself how to call other custom functions:

func main() {

	getCurrentVersion()

}

JSON parsing function

Finally our JSON parsing Go function:

func getCurrentVersion() {

	file, err := ioutil.ReadFile("appinfo.json")

	if err != nil {
		fmt.Println(err)
	}

	data := ApplicationNodes{}

	_ = json.Unmarshal([]byte(file), &data)

	for i := 0; i < len(data.ApplicationNodes); i++ {

		fmt.Println(data.ApplicationNodes[i].Developer, "-", data.ApplicationNodes[i].AppName, "(v"+data.ApplicationNodes[i].Version, "-", data.ApplicationNodes[i].ReleaseDate+")")

	}

}

Test JSON parsing

We are now done with our application code, lets run it and find out if it works:

go run main.go

A successful output should look like this:

John Doe Inc - Parsing JSON File with Golang (v0.0.1 - 05/20/2019)

That is all, our short tutorial about parsing JSON files in Go ends here.

Video

No video posted for this page.

Screenshots

No screenshots posted for this page.

Source code

No code posted for this page.

About this page

Article
Parsing JSON file with Go
Author
Category
Published
30/05/2019
Updated
19/07/2019
Tags

Share this page

If you found this page useful please share it with your friends or colleagues.