Closed lefscode closed 6 years ago
Probably we should add this feature soon.
Thanks for the follow up. I think that this would be very helpful
.... From("data").Where("fid", "=", 10).Where("children" "OP", number).
Where OP could be count, contains, max, min, etc....
.... From("data").Where("fid", "=", 10).Where("children" "count", 2)
@lefscode For this time you can add custom Macro function to solve the problem
@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())
}
As this type of task can be done by
Macro
I'm closing the issue.
@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