Skip to content

Commit

Permalink
Fix parsing JSON example and add it into action (#152)
Browse files Browse the repository at this point in the history
* Fix parsing JSON example and add it into action

* Fix port in the example
  • Loading branch information
Aiee committed Nov 2, 2021
1 parent 541992f commit 389d9e6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 22 additions & 4 deletions json_example/parse_json_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit 389d9e6

Please sign in to comment.