diff --git a/Makefile b/Makefile index 76d39677..d79a7a4d 100644 --- a/Makefile +++ b/Makefile @@ -40,4 +40,5 @@ run-examples: cd .. && \ go run basic_example/graph_client_basic_example.go && \ go run gorountines_example/graph_client_goroutines_example.go && \ + go run json_example/parse_json_example.go && \ cd ./nebula-docker-compose && docker-compose down -v diff --git a/json_example/parse_json_example.go b/json_example/parse_json_example.go index 50667802..74738900 100644 --- a/json_example/parse_json_example.go +++ b/json_example/parse_json_example.go @@ -18,7 +18,7 @@ const ( address = "127.0.0.1" // The default port of Nebula Graph 2.x is 9669. // 3699 is only for testing. - port = 29562 + port = 3699 username = "root" password = "nebula" ) @@ -65,8 +65,8 @@ type JsonObj struct { Comment string `json:"comment "` } `json:"results"` Errors []struct { - ErrorCode int `json:"errorCode"` - ErrorMsg string `json:"errorMsg"` + Code int `json:"code"` + Message string `json:"message"` } `json:"errors"` } @@ -118,7 +118,25 @@ func main() { // Get a property birthday := jsonObj.Results[0].Data[0].Row[0].(map[string]interface{})["person.birthday"] - fmt.Printf("person.birthday is %s \n", birthday) + fmt.Printf("person.birthday is %s \n\n", birthday) + } + // With error + { + jsonStrResult, err := session.ExecuteJson("MAT (v:person {name: \"Bob\"}) RETURN v") + if err != nil { + log.Fatal(fmt.Sprintf("fail to get the result in json format, %s", err.Error())) + } + + var jsonObj JsonObj + // Parse JSON + json.Unmarshal(jsonStrResult, &jsonObj) + // Get error message + errorMsg := jsonObj.Errors[0].Message + msg, err := json.MarshalIndent(errorMsg, "", " ") + if err != nil { + fmt.Println("error:", err) + } + fmt.Println("Error message: ", string(msg)) } dropSpace(session, "client_test")