Skip to content
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

[question] examples of usage? #22

Open
arvenil opened this issue Feb 10, 2016 · 4 comments
Open

[question] examples of usage? #22

arvenil opened this issue Feb 10, 2016 · 4 comments
Labels

Comments

@arvenil
Copy link

arvenil commented Feb 10, 2016

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)?

@jamesls
Copy link
Member

jamesls commented Aug 3, 2016

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(), ...?

@JalfResi
Copy link

JalfResi commented Feb 6, 2017

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)
}

@arvenil
Copy link
Author

arvenil commented Feb 6, 2017

@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!

@suntong
Copy link

suntong commented Jun 2, 2018

I agree that this can be closed now, with @JalfResi posted example.
Is the issue board closely monitored?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants