-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: Use GJSON in TB codes #312
Comments
Issue-Label Bot is automatically applying the label Links: app homepage, dashboard and code for this bot. |
@jihoon-seo 전체적으로 적용하는 것이 좋을까요? 일단 저는 CB-DF 의 모니터링 데이터 조회를 위해서 활용해보았습니다~ cb-tumblebug/src/core/mcis/monitor.go Lines 373 to 389 in 0e2da36
|
|
Spider 가 제공한 & TB가 저장하고 있는 일단 얘가 눈에 띕니다. jsonParsed, err := gabs.ParseJSON([]byte(`{
"outter":{
"inner":{
"value1":10,
"value2":22
},
"alsoInner":{
"value1":20,
"array1":[
30, 40
]
}
}
}`))
if err != nil {
panic(err)
}
var value float64
var ok bool
value, ok = jsonParsed.Path("outter.inner.value1").Data().(float64)
// value == 10.0, ok == true
value, ok = jsonParsed.Search("outter", "inner", "value1").Data().(float64)
// value == 10.0, ok == true
value, ok = jsonParsed.Search("outter", "alsoInner", "array1", "1").Data().(float64)
// value == 40.0, ok == true
gObj, err := jsonParsed.JSONPointer("/outter/alsoInner/array1/1")
if err != nil {
panic(err)
}
value, ok = gObj.Data().(float64)
// value == 40.0, ok == true
value, ok = jsonParsed.Path("does.not.exist").Data().(float64)
// value == 0.0, ok == false
exists := jsonParsed.Exists("outter", "inner", "value1")
// exists == true
exists = jsonParsed.ExistsP("does.not.exist")
// exists == false 얘도 눈에 띕니다. data := []byte(`{
"person": {
"name": {
"first": "Leonid",
"last": "Bugaev",
"fullName": "Leonid Bugaev"
},
"github": {
"handle": "buger",
"followers": 109
},
"avatars": [
{ "url": "https://avatars1.githubusercontent.com/u/14009?v=3&s=460", "type": "thumbnail" }
]
},
"company": {
"name": "Acme"
}
}`)
// You can specify key path by providing arguments to Get function
jsonparser.Get(data, "person", "name", "fullName")
// There is `GetInt` and `GetBoolean` helpers if you exactly know key data type
jsonparser.GetInt(data, "person", "github", "followers")
// When you try to get object, it will return you []byte slice pointer to data containing it
// In `company` it will be `{"name": "Acme"}`
jsonparser.Get(data, "company") 근데 이 정도는 GJSON 도 비슷하게 가능할 것 같기도 하네요 🤔 |
Iteration을 고려하신다면 GJSON이 좋을 것 같다는 의견을 드립니다 😄 JSON 데이터 구조에 집중할 수 있고, 몇 줄로 원하는 데이터(string, array, object 등)를 파싱하기 용이해서 입니다. 참고:
result := gjson.Get(json, "programmers.#.lastName")
for _, name := range result.Array() {
println(name.String())
}
name := gjson.Get(json, `programmers.#(lastName="Hunter").firstName`)
println(name.String()) // prints "Elliotte" |
Found by @seokho-son
https://github.com/tidwall/gjson
The text was updated successfully, but these errors were encountered: