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

Support empty paths #322

Closed
becheran opened this issue Mar 31, 2023 · 2 comments
Closed

Support empty paths #322

becheran opened this issue Mar 31, 2023 · 2 comments

Comments

@becheran
Copy link

Are empty paths are also supported? I don't really find an answer to the question in the path documentation. The following test fails for me, but I would have expected it to succeed if empty paths would have been allowed? I Would have expected that for empty paths the full json payload would be interpreted.

func TestEmptyPath(t *testing.T) {
    testrunner.UnitTest(t, func() {
        res := gjson.Get(`["foo"]`, "")
        if !res.Exists(){
            t.Fail()
        }
    })
}
@tidwall
Copy link
Owner

tidwall commented Mar 31, 2023

An empty path is valid and will return the value for an empty key in an object.

gjson.Get(`{ "": "hi" }`, "").String() == "hi"
gjson.Get(`{ "": { "inner": "hi" } }`, ".inner").String() == "hi"

@becheran
Copy link
Author

@tidwall ah got it. Thx for the fast response and clarification!

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

No branches or pull requests

2 participants