05 How to know JSON Array
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
1func json.Marshal(interface{})And for conversion into data in Golang using
1func 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.
1type Customer struct {
2 FirstName string `json:"first_name"`
3 MiddleName string `json:"middle_name"`
4 LastName string `json:"last_name"`
5 Hobbies []string `json:"hobbies"`
6}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.
1--- FAIL: TestGenerateObjectJSON (0.00s)
2 --- FAIL: TestGenerateObjectJSON/success_generate_object_JSON (0.00s)
3 /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"}
4FAIL
5FAIL github.com/santekno/learn-golang-json 0.498s
6FAILWe need to change the unit test so that it can support the array data contained in the Customer struct to be as shown below.
1func TestGenerateObjectJSON(t *testing.T) {
2 type args struct {
3 data Customer
4 }
5 tests := []struct {
6 name string
7 args args
8 want string
9 }{
10 {
11 name: "success generate object JSON",
12 args: args{
13 data: Customer{
14 FirstName: "Santekno",
15 MiddleName: "Ihsan",
16 LastName: "Arif",
17 Hobbies: []string{"badminton", "renang", "coding"},
18 },
19 },
20 want: string(`{"first_name":"Santekno","middle_name":"Ihsan","last_name":"Arif","hobbies":["badminton","renang","coding"]}`),
21 },
22 }
23 for _, tt := range tests {
24 t.Run(tt.name, func(t *testing.T) {
25 if got := GenerateObjectJSON(tt.args.data); got != tt.want {
26 t.Errorf("GenerateObjectJSON() = %v, want %v", got, tt.want)
27 }
28 })
29 }
30}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.
1func TestConvertObjectJSON(t *testing.T) {
2 type args struct {
3 data string
4 }
5 tests := []struct {
6 name string
7 args args
8 want Customer
9 }{
10 {
11 name: "success conversion object JSON",
12 args: args{
13 data: string(`{"first_name":"Santekno","middle_name":"Ihsan","last_name":"Arif","hobbies":["badminton","renang","coding"]}`),
14 },
15 want: Customer{
16 FirstName: "Santekno",
17 MiddleName: "Ihsan",
18 LastName: "Arif",
19 Hobbies: []string{"badminton", "renang", "coding"},
20 },
21 },
22 }
23 for _, tt := range tests {
24 t.Run(tt.name, func(t *testing.T) {
25 if got := ConvertObjectJSON(tt.args.data); !reflect.DeepEqual(got, tt.want) {
26 t.Errorf("ConvertObjectJSON() = %v, want %v", got, tt.want)
27 }
28 })
29 }
30}