Introduction to JSON Arrays
Apart from JSON in Object form, usually in JSON we can also use the Array data type. Actually, JSON arrays are similar to those in JavaScript where there are primitive data types or complex data types such as object arrays.
In Golang, this JSON Array is represented in slice form which later when we convert from JSON or to JSON it will be done automatically using the json package using the slice data type. In terms of usage, the function used is the same as the previous function, namely when creating JSON
func json.Marshal(interface{})
And for conversion into data in Golang using
func json.Unmarshal(byte[],interface{})
How to Implement
For example, we will try to add an array data type to the struct that we created previously, as shown below.
type Customer struct {
FirstName string `json:"first_name"`
MiddleName string `json:"middle_name"`
LastName string `json:"last_name"`
Hobbies []string `json:"hobbies"`
}
We try to run a unit test for what was previously created, namely the TestGenerateObjectJSON
function. Then the unit test will show FAIL
because there is a new data type that we added.
--- FAIL: TestGenerateObjectJSON (0.00s)
--- FAIL: TestGenerateObjectJSON/success_generate_object_JSON (0.00s)
/Users/ihsanarif/Documents/ihsan/tutorial/learn-golang-json/main_test.go:96: GenerateObjectJSON() = {"first_name":"Santekno","middle_name":"Ihsan","last_name":"Arif","hobbies":null}, want {"first_name":"Santekno","middle_name":"Ihsan","last_name":"Arif"}
FAIL
FAIL github.com/santekno/learn-golang-json 0.498s
FAIL
We need to change the unit test so that it can support the array data contained in the Customer
struct to be as shown below.
func TestGenerateObjectJSON(t *testing.T) {
type args struct {
data Customer
}
tests := []struct {
name string
args args
want string
}{
{
name: "success generate object JSON",
args: args{
data: Customer{
FirstName: "Santekno",
MiddleName: "Ihsan",
LastName: "Arif",
Hobbies: []string{"badminton", "renang", "coding"},
},
},
want: string(`{"first_name":"Santekno","middle_name":"Ihsan","last_name":"Arif","hobbies":["badminton","renang","coding"]}`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GenerateObjectJSON(tt.args.data); got != tt.want {
t.Errorf("GenerateObjectJSON() = %v, want %v", got, tt.want)
}
})
}
}
In the unit test function TestConvertObjectJSON
there is no FAIL
because for marshal if it is not set the JSON will not appear as a key in the JSON object. But so that everyone is uniform, we also change this unit test to something like the one below.
func TestConvertObjectJSON(t *testing.T) {
type args struct {
data string
}
tests := []struct {
name string
args args
want Customer
}{
{
name: "success conversion object JSON",
args: args{
data: string(`{"first_name":"Santekno","middle_name":"Ihsan","last_name":"Arif","hobbies":["badminton","renang","coding"]}`),
},
want: Customer{
FirstName: "Santekno",
MiddleName: "Ihsan",
LastName: "Arif",
Hobbies: []string{"badminton", "renang", "coding"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ConvertObjectJSON(tt.args.data); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ConvertObjectJSON() = %v, want %v", got, tt.want)
}
})
}
}