diff --git a/pkg/types/json_binary_functions.go b/pkg/types/json_binary_functions.go index 644df34b59a78..b3c05da7612ca 100644 --- a/pkg/types/json_binary_functions.go +++ b/pkg/types/json_binary_functions.go @@ -277,13 +277,13 @@ func (bj BinaryJSON) extractTo(buf []BinaryJSON, pathExpr JSONPathExpression, du if currentLeg.typ == jsonPathLegArraySelection { if bj.TypeCode != JSONTypeCodeArray { // If the current object is not an array, still append them if the selection includes - // 0. But for asterisk, it still returns NULL. + // 0 or last. But for asterisk, it still returns NULL. // // don't call `getIndexRange` or `getIndexFromStart`, they will panic if the argument // is not array. switch selection := currentLeg.arraySelection.(type) { case jsonPathArraySelectionIndex: - if selection.index == 0 { + if selection.index == 0 || selection.index == -1 { buf = bj.extractTo(buf, subPathExpr, dup, one) } case jsonPathArraySelectionRange: diff --git a/tests/integrationtest/r/expression/json.result b/tests/integrationtest/r/expression/json.result index e5dc69ff1961f..5339227f32e11 100644 --- a/tests/integrationtest/r/expression/json.result +++ b/tests/integrationtest/r/expression/json.result @@ -852,3 +852,12 @@ json_extract("{\"\\b\":\"\"}", "$") select json_extract("{\"\\f\":\"\"}", "$"); json_extract("{\"\\f\":\"\"}", "$") {"\f": ""} +select json_extract('{"a":"b"}', '$[0]'); +json_extract('{"a":"b"}', '$[0]') +{"a": "b"} +select json_extract('{"a":"b"}', '$[last]'); +json_extract('{"a":"b"}', '$[last]') +{"a": "b"} +select json_set('{"a":"b"}', '$[last]', 1); +json_set('{"a":"b"}', '$[last]', 1) +1 diff --git a/tests/integrationtest/t/expression/json.test b/tests/integrationtest/t/expression/json.test index 4df8140df7cb5..d4af74bd27372 100644 --- a/tests/integrationtest/t/expression/json.test +++ b/tests/integrationtest/t/expression/json.test @@ -560,3 +560,8 @@ select json_extract('[1E27]', '$'); # TestIssue58897 select json_extract("{\"\\b\":\"\"}", "$"); select json_extract("{\"\\f\":\"\"}", "$"); + +# TestJSONExtractObjectFromLast +select json_extract('{"a":"b"}', '$[0]'); +select json_extract('{"a":"b"}', '$[last]'); +select json_set('{"a":"b"}', '$[last]', 1); \ No newline at end of file