thedevsaddam/gojsonq

how to extract child element after using find

kilometer2162 opened this issue · 1 comments

package main

import (
"fmt"
"github.com/thedevsaddam/gojsonq"
"log"
)
func main(){
jsonStr := {"code":0,"data":{"tick":{"bids":[[0.5,0.335],[0.45,0.235],[0.4,0.35]],"asks":[[0.6,22.01],[0.61,0.52],[0.63,0.35]]}}}
grapArg := GrapArg{path:"data.tick",keyBids:"bids",keyAsks:"asks",keyPrice:"0",keyAmount:"1"}
getDepthByPath(jsonStr,grapArg)
}

func getDepthByPath(jsonStr string,grapArg GrapArg)([][]float32,[][]float32){
jq := gojsonq.New().FromString(jsonStr)
path := grapArg.path
if len(path)>0{
data := jq.Find(path)
if jq.Error() != nil {
log.Fatal(jq.Errors())
}
fmt.Println(data)

}
return [][]float32{{0.0000000000000001},{0.0001}}, [][]float32{{1000000},{0.0001}}

}

type GrapArg struct {
path string
keyBids string
keyAsks string
keyPrice string
keyAmount string
}

// I want to extract elements "bids" and "asks" , how to do ? help me if convenient please!

You can use Out method.

package main

import (
	"fmt"
	"reflect"

	"github.com/thedevsaddam/gojsonq"
)

type item struct {
	Bids [][]float64 `json:"bids"`
	Asks [][]float64 `json:"asks"`
}

const json = `{"code":0,"data":{"tick":{"bids":[[0.5,0.335],[0.45,0.235],[0.4,0.35]],"asks":[[0.6,22.01],[0.61,0.52],[0.63,0.35]]}}}`

func main() {
	v := item{}
	gojsonq.New().JSONString(json).From("data.tick").Out(&v)
	fmt.Printf("%#v\n", v)
}

Output

main.item{Bids:[][]float64{[]float64{0.5, 0.335}, []float64{0.45, 0.235}, []floa
t64{0.4, 0.35}}, Asks:[][]float64{[]float64{0.6, 22.01}, []float64{0.61, 0.52},
[]float64{0.63, 0.35}}}