Create JSON Files in Golang

Who needs a custom reporter for automation with JSON format?

 

Sometimes we need the specific format of the reporter we’re using because the tools don’t provide the format we normally use. Don’t worry because many programming languages actually provide support to create JSON format and we could save it to .json file.

Pssst: we can use it to save our automation run result to another format besides the Cucumber JSON report.

Install Golang

Ensure we have installed Golang version in our local computer.

Create folder

Please create golang-api-json folder for our playground this time.

Create main.go

Basically in Golang, the init file should be named by main.go but I think it’s up to us for using another naming.

 

So here I’ll be using JSON custom format because I don’t use Cucumber for my API automation.

The actual format we need is like this.

{
  "testExecutionKey": "TEST-01",
  "tests" : [{
    "testKey" : "TEST-02",
    "comment" : "mpermperpisang",
    "status" : "PASSED"
  },
  {
    "testKey" : "TEST-03",
    "comment" : "mpermperpisang",
    "status" : "FAILED"
  }]
}

In Golang, we can use struct to define the JSON structure.

  • First we create struct for array value inside "tests" key.
type testCase struct {
	Key     string `json:"testKey"`
	Comment string `json:"comment"`
	Status  string `json:"status"`
}
  • We have to call the testCase struct as the new type of data. Take a look at []*testCase , it’s because we need the content to be in array format inside "tests" : key.
type importTestCase struct {
	Execution string      `json:"testExecutionKey"`
	Tests     []*testCase `json:"tests"`
}

The init code in main.go file have to be like this.

package main

type importTestCase struct {
	Execution string      `json:"testExecutionKey"`
	Tests     []*testCase `json:"tests"`
}

type testCase struct {
	Key     string `json:"testKey"`
	Comment string `json:"comment"`
	Status  string `json:"status"`
}

func main() {
  // still empty
}

I want to input custom value to define the Test Execution ID. So, we can use Golang Flag standard library => https://pkg.go.dev/flag.

package main

import (
	"flag"
	"fmt"
)

type importTestCase struct {
	Execution string      `json:"testExecutionKey"`
	Tests     []*testCase `json:"tests"`
}

type testCase struct {
	Key     string `json:"testKey"`
	Comment string `json:"comment"`
	Status  string `json:"status"`
}

func main() {
	args := flag.String("exec", "TEST-01", "Input test execution ID")
	flag.Parse()

	fmt.Println(*args)
}

Run it by:

go run main.go(it will print TEST-01as default value for Test Execution ID) or go run main.go -exec=TEST-100 (it will print TEST-100as custom value for Test Execution ID)

I will input dummy value for the array type and do the loop if the array index is more than 0.

package main

import (
	"encoding/json"
	"fmt"
	"strings"
)

type importTestCase struct {
	Execution string      `json:"testExecutionKey"`
	Tests     []*testCase `json:"tests"`
}

type testCase struct {
	Key     string `json:"testKey"`
	Comment string `json:"comment"`
	Status  string `json:"status"`
}

func main() {
	var cases []*testCase

	line := `
TEST-02 mpermperpisang PASSED
TEST-03 mpermperpisang FAILED
`

	for _, line := range strings.Split(line, "\n") {
		if line != "" {
			s := strings.Split(line, " ")
			cases = append(cases,
				&testCase{
					Key:     s[0],
					Comment: s[1],
					Status:  s[2],
				},
			)
		}
	}

	jsonSample, _ := json.MarshalIndent(cases, "", "  ")

	fmt.Println(string(jsonSample))
}

It will print out the JSON like below.

To print full JSON of the custom format above, change code json.MarshalIndent(cases), "", " ") to code below.

jsonSample, _ := json.MarshalIndent(importTestCase{
  Execution: *args,
  Tests:     cases,
 }, "", "  ")

After we ensure the JSON format is correct, next step is to save the JSON to .json file. We’ll use this standard library from Golang => https://pkg.go.dev/io/ioutil#example-WriteFile.

package main

import (
	"encoding/json"
	"flag"
	"io/ioutil"
	"strings"
)

type importTestCase struct {
	Execution string      `json:"testExecutionKey"`
	Tests     []*testCase `json:"tests"`
}

type testCase struct {
	Key     string `json:"testKey"`
	Comment string `json:"comment"`
	Status  string `json:"status"`
}

func main() {
	var cases []*testCase
	args := flag.String("exec", "TEST-01", "Input test execution ID")
	flag.Parse()

	line := `
TEST-02 mpermperpisang PASSED
TEST-03 mpermperpisang FAILED
`

	for _, line := range strings.Split(line, "\n") {
		if line != "" {
			s := strings.Split(line, " ")
			cases = append(cases,
				&testCase{
					Key:     s[0],
					Comment: s[1],
					Status:  s[2],
				},
			)
		}
	}

	jsonSample, _ := json.MarshalIndent(importTestCase{
		Execution: *args,
		Tests:     cases,
	}, "", "  ")

	ioutil.WriteFile("automation_api_result.json", jsonSample, 0644)
}

https://www.itential.com/wp-content/uploads/2022/09/Blog_Featured_Defining-JSON-Schema-Why-Its-the-Preferred-Automation-Data-Language-01.jpg

As long we can create JSON format, don’t worry about automation running result. — MperMperPisang


#go #json #golang 

Create JSON Files in Golang
1.00 GEEK