Parsing JSON in GO is an Adventure

One of the tough things to do in Golang is parsing JSON! Yes, indeed its a challenge for novice like me. When it comes to Python and Ruby its an easy task thanks to JSON libraries which are pretty easy to use especially in Python.

I tried to create the struct for a complex and nested JSON data below by hands but was not succeeded after several attempts.

{
    "clients": [
        {
            "clientId": "dde46983-00000004-5cdac62e-5cdc1fc1-00025000-a4aa9156",
            "hostname": "A999US032WIN001",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/159.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "159.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 24
            }
        },
        {
            "clientId": "70f32834-00000004-5cdac630-5cdd6bf5-00195000-a4aa9156",
            "hostname": "a999us034cen001",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/170.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "170.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 6
            }
        },
        {
            "clientId": "e34f2de3-00000004-5cdac62d-5cdac62c-00015000-a4aa9156",
            "hostname": "a999us034nve001.usp01.xstream360.cloud",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/158.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "158.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 2
            }
        },
        {
            "clientId": "3084d369-00000004-5cdac62f-5cdd4e05-000e5000-a4aa9156",
            "hostname": "a999us034rhl001",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/167.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "167.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 6
            }
        },
        {
            "clientId": "82fa0d80-00000004-5cdac631-5ceb7989-01165000-a4aa9156",
            "hostname": "icehousetest",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/172.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "172.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 1
            }
        },
        {
            "clientId": "82fa0d80-00000004-5cdac631-5ceb7989-01165000-a4aa9156",
            "hostname": "icehousetest",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/173.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "173.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 1
            }
        },
        {
            "clientId": "82fa0d80-00000004-5cdac631-5ceb7989-01165000-a4aa9156",
            "hostname": "icehousetest",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/174.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "174.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 1
            }
        },
        {
            "clientId": "82fa0d80-00000004-5cdac631-5ceb7989-01165000-a4aa9156",
            "hostname": "icehousetest",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/176.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "176.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 1
            }
        },
        {
            "clientId": "82fa0d80-00000004-5cdac631-5ceb7989-01165000-a4aa9156",
            "hostname": "icehousetest",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/175.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "175.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 1
            }
        },
        {
            "clientId": "82fa0d80-00000004-5cdac631-5ceb7989-01165000-a4aa9156",
            "hostname": "icehousetest",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/177.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "177.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 1
            }
        },
        {
            "clientId": "82fa0d80-00000004-5cdac631-5ceb7989-01165000-a4aa9156",
            "hostname": "icehousetest",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/180.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "180.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 1
            }
        },
        {
            "clientId": "82fa0d80-00000004-5cdac631-5ceb7989-01165000-a4aa9156",
            "hostname": "icehousetest",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/178.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "178.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 1
            }
        },
        {
            "clientId": "82fa0d80-00000004-5cdac631-5ceb7989-01165000-a4aa9156",
            "hostname": "icehousetest",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/187.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "187.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 1
            }
        },
        {
            "clientId": "82fa0d80-00000004-5cdac631-5ceb7989-01165000-a4aa9156",
            "hostname": "icehousetest",
            "links": [
                {
                    "href": "https://10.10.10.6:9090/nwrestapi/v2/global/clients/184.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                    "rel": "item"
                }
            ],
            "resourceId": {
                "id": "184.0.129.30.0.0.0.0.20.198.218.92.10.10.10.6",
                "sequence": 1
            }
        }
    ],
    "count": 14
}

After deep (re)searching using DuckDuckGo, I finally found an easier way to convert JSON to Structs with the help of JSON-to-Go tool. Many thanks to Matt Holt for making things simple for me.

Here is the screenshot of converting JSON to Strcuts. This is the major step which was made very easy using this tool. By now you we know that we should have access to JSON data which should be static and available locally for conversion.

Now the required strcuts and types are autocreated, its time to unmarshal JSON data to strcuts and access its values. Below is the full implementation of the code.

/*
Parse and convert JSON to structs using JSON-to-Go Tool
*/
package main

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

// structs generated using https://mholt.github.io/json-to-go/
type AutoGenerated struct {
	Clients []struct {
		ClientID string `json:"clientId"`
		Hostname string `json:"hostname"`
		Links    []struct {
			Href string `json:"href"`
			Rel  string `json:"rel"`
		} `json:"links"`
		ResourceID struct {
			ID       string `json:"id"`
			Sequence int    `json:"sequence"`
		} `json:"resourceId"`
	} `json:"clients"`
	Count int `json:"count"`
}

func main() {
	var info AutoGenerated
	// Reading data from JSON File
	file, e := ioutil.ReadFile("clients.json")
	if e != nil {
		fmt.Printf("File error: %v\n", e)
		os.Exit(1)
	}
	//Unmarshal json data into struct info
	if err := json.Unmarshal(file, &info); err != nil {
		log.Fatal(err)
	}

	//fmt.Printf("%+v\n", info)
	fmt.Println("CLIENT-ID,HOSTNAME,RESOURCE-ID")

	//Iterate through each value and print required types
	for _, value := range info.Clients {
		fmt.Printf("%s,%s,%s\n", value.ClientID, value.Hostname, value.ResourceID.ID)
	}

}

Hope the above example code would help you to understand the steps to convert JSON to Structs, iterate through each key value and access those required types. Please leave your feedback if you found this useful and suggestions in the below Comments section.

Advertisements

Written by Vinay

Proud and Tolerant Indian

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s