-
Notifications
You must be signed in to change notification settings - Fork 140
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
Query json for array size. #22
Comments
Probably we should add this feature soon. |
Thanks for the follow up. I think that this would be very helpful Where OP could be count, contains, max, min, etc.... |
@thedevsaddam, thanks. That's interesting. How can we get the existence of the "children" array and the capacity of this array? |
@lefscode Here is a quick solution, please check errors and more types for your need. package main
import (
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/thedevsaddam/gojsonq"
)
const json = `{
"data":[
{
"fid":10,
"children":[
{
"cid":121
},
{
"cid":122
},
{
"cid":125
}
]
},
{
"fid":10,
"children":[
{
"cid":123
},
{
"cid":124
}
]
}
]
}`
func main() {
var count = func(x, y interface{}) (bool, error) {
yv, ok := y.(int)
if !ok {
return false, fmt.Errorf("%v is must be an integer", y)
}
xv, ok := x.([]interface{}) // do various check for yourself
if !ok {
return false, fmt.Errorf("%v must be an array", x)
}
if len(xv) == yv {
return true, nil
}
return false, nil
}
q := gojsonq.New().Macro("count", count).JSONString(json).From("data").
Where("fid", "=", 10).Where("children", "count", 3)
spew.Dump(q.Get(), q.Error())
}
|
@lefscode This is a good feature. You can use it from gojsonq v1.7.0 |
Hello,
This is an "info request".
I have the following json and I would like to check if the input contains a father (fid) with two children.
The code so far is
How can I modify the source code to decide if fid=10 AND children.size() = 2?
In principle how can I use the "array size" to query the json file/input?
Thanks
The text was updated successfully, but these errors were encountered: