[question] examples of usage?
Hi, I mostly wonder what kind of data jmespath.Search() expects?
Param data says interface{} so it's not really helpful... can I use string {} or []byte("{}") or what it should be?
From testcases I see that map[string]interface{} is used so it looks like output of json.Unmarshal... but is this the only data type that can be passed? Or maybe json string is allowed too?
Same questions apply to return value... what kind of data types I can expect? interface{} type suggest I can expect various data types, or maybe even self defined ones... but there is no info about it... so no idea what should I try to assert.
I was trying to do something simple like:
v, err := jmespath.Search("a.b", `{"a":{"b":"test"}}`)
log.Println(v)
but this doesn't seem to work
Also, considering jmespath.Search() returns interface{} then beside type assertion is there any nicer way to convert this some usable data type? Like string or []byte or self defined struct (e.g. like with json.Unmarshal)?
Sorry for the delay here. The type of data it expects is as you've mentioned: the output of json.Unmarshal. JSON strings are not currently supported, but it is something that I might consider adding as a separate function.
The return value right now will depend on the what the expression is, so for example, your expression above of a.b with the input of json.Unmarshal of {"a": {"b": "test"}} will return "test". However if you instead had {"a":{"b": {"c": "foo"}}}, it will return {"c": "foo"} instead.
I'm certainly open to suggestions to improve this. Would you want something like .Search() returns a Result type, with corresponding .String(), ... functions? Or perhaps something like .SearchString(), ...?
The following works for me:
package main
import (
"encoding/json"
"log"
"github.com/jmespath/go-jmespath"
)
func main() {
var jsonBlob = []byte(`{"name":"ben", "items": [{"age":38}]}`)
var d interface{}
err := json.Unmarshal(jsonBlob, &d)
if err != nil {
log.Fatal(err)
}
v, err := jmespath.Search("items[0].age", d)
if err != nil {
log.Fatal(err)
}
log.Println(v)
}
@jamesls Hard to tell to be honest... after a year I totally don't remember what I needed it for :) Please feel free to close the issue. I think what was important for me at that time was the example how to use. Something like @JalfResi posted. I believe it would be nice to put it in README.md.
@JalfResi thanks for the example!
I agree that this can be closed now, with @JalfResi posted example. Is the issue board closely monitored?