diff --git a/src/commands/cmd_json.cc b/src/commands/cmd_json.cc index 196b3bcfce1..86017fb514e 100644 --- a/src/commands/cmd_json.cc +++ b/src/commands/cmd_json.cc @@ -174,7 +174,9 @@ class CommandJsonObjkeys : public Commander { std::vector>> results; - auto s = json.ObjKeys(args_[1], args_[2], results); + // If path not specified set it to $ + std::string path = (args_.size() > 2) ? args_[2] : "$"; + auto s = json.ObjKeys(args_[1], path, results); if (!s.ok() && !s.IsNotFound()) return {Status::RedisExecErr, s.ToString()}; if (s.IsNotFound()) { *output = redis::NilString(); diff --git a/tests/gocase/unit/type/json/json_test.go b/tests/gocase/unit/type/json/json_test.go index d87e9032d5b..b5d741cd510 100644 --- a/tests/gocase/unit/type/json/json_test.go +++ b/tests/gocase/unit/type/json/json_test.go @@ -207,8 +207,9 @@ func TestJson(t *testing.T) { require.NoError(t, rdb.Do(ctx, "JSON.SET", "a", "$", `{"a1":[1,2]}`).Err()) require.EqualValues(t, []interface{}{}, rdb.Do(ctx, "JSON.OBJKEYS", "a", "$.not_exists").Val()) // json path not object - require.NoError(t, rdb.Do(ctx, "JSON.SET", "a", "$", `{"a1":[1,2]}`).Err()) require.EqualValues(t, []interface{}{nil}, rdb.Do(ctx, "JSON.OBJKEYS", "a", "$.a1").Val()) + // default path + require.EqualValues(t, []interface{}{[]interface{}{"a1"}}, rdb.Do(ctx, "JSON.OBJKEYS", "a").Val()) // json path has one object require.NoError(t, rdb.Do(ctx, "JSON.SET", "a", "$", `{"a1":{"b":1,"c":1}}`).Err()) require.EqualValues(t, []interface{}{[]interface{}{"b", "c"}}, rdb.Do(ctx, "JSON.OBJKEYS", "a", "$.a1").Val())