diff --git a/parser.go b/parser.go index cee8f91..ca6d4bc 100644 --- a/parser.go +++ b/parser.go @@ -195,7 +195,11 @@ func searchKeys(data []byte, keys ...string) int { if valueFound == nil { return -1 } else { - return i + valueOffset + searchKeys(valueFound, keys[level+1:]...) + subIndex := searchKeys(valueFound, keys[level+1:]...) + if subIndex < 0 { + return -1 + } + return i + valueOffset + subIndex } } else { // Do not search for keys inside arrays diff --git a/parser_test.go b/parser_test.go index 89be330..5f5ab66 100644 --- a/parser_test.go +++ b/parser_test.go @@ -357,7 +357,19 @@ var getTests = []GetTest{ path: []string{"b"}, isFound: false, }, - + { // Issue #81 + desc: `missing key in object in array`, + json: `{"p":{"a":[{"u":"abc","t":"th"}]}}`, + path: []string{"p", "a", "[0]", "x"}, + isFound: false, + }, + { // Issue #81 counter test + desc: `existing key in object in array`, + json: `{"p":{"a":[{"u":"abc","t":"th"}]}}`, + path: []string{"p", "a", "[0]", "u"}, + isFound: true, + data: "abc", + }, { // This test returns not found instead of a parse error, as checking for the malformed JSON would reduce performance desc: "malformed key (followed by comma followed by colon)", json: `{"a",:1}`,