tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
13.88k stars 841 forks source link

Support empty paths #322

Closed becheran closed 1 year ago

becheran commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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