{
  "schema_version": 2,
  "id": "commands/hget",
  "title": "HGET",
  "url": "https://redis.io/docs/latest/commands/hget/",
  "summary": "Returns the value of a field in a hash.",
  "since": "2.0.0",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-07-08T12:05:04+02:00",
  "page_type": "content",
  "content_hash": "d0d4bd081067dac6fe7c9804c07cf31f004fa5418d871a573a047ece0bd6fc0a",
  "sections": [
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>key</code></summary>\n\nThe name of the key that holds the hash.\n\n</details>\n\n<details open><summary><code>field</code></summary>\n\nThe field whose value to retrieve.\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "Foundational: Retrieve a single field value from a hash using HGET (returns nil if field or key doesn't exist)\n\n**Difficulty:** Beginner\n\n**Commands:** HSET, HGET\n\n**Complexity:**\n- HSET: O(1)\n- HGET: O(1)\n\n**Available in:** Redis CLI, C, C#, Go, Java, Java (Asynchronous - Lettuce), Java (Reactive - Lettuce), Java (Synchronous - Jedis), JavaScript (Node.js), JavaScript (Node.js), PHP, Python, Ruby, Rust (Asynchronous), Rust (Synchronous)\n\n##### Redis CLI\n\n[code example]\n\n##### C\n\n[code example]\n\n##### C#\n\n[code example]\n\n##### Go\n\n[code example]\n\n##### Java\n\n[code example]\n\n##### Java (Asynchronous - Lettuce)\n\n[code example]\n\n##### Java (Reactive - Lettuce)\n\n[code example]\n\n##### Java (Synchronous - Jedis)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### PHP\n\n[code example]\n\n##### Python\n\n[code example]\n\n##### Ruby\n\n[code example]\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]"
    },
    {
      "id": "redis-software-and-redis-cloud-compatibility",
      "title": "Redis Software and Redis Cloud compatibility",
      "role": "content",
      "text": "| Redis<br />Software | Redis<br />Cloud | <span style=\"min-width: 9em; display: table-cell\">Notes</span> |\n|:----------------------|:-----------------|:------|\n| <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> | <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\nOne of the following:\n* [Bulk string reply](../../develop/reference/protocol-spec#bulk-strings): The value associated with the field.\n* [Nil reply](../../develop/reference/protocol-spec#bulk-strings): If the field is not present in the hash or key does not exist.\n\n**RESP3:**\n\nOne of the following:\n* [Bulk string reply](../../develop/reference/protocol-spec#bulk-strings): The value associated with the field.\n* [Null reply](../../develop/reference/protocol-spec#nulls): If the field is not present in the hash or key does not exist."
    }
  ],
  "examples": [
    {
      "id": "examples-ex0",
      "language": "plaintext",
      "code": "> HSET myhash field1 \"foo\"\n(integer) 1\n> HGET myhash field1\n\"foo\"\n> HGET myhash field2\n(nil)",
      "section_id": "examples"
    },
    {
      "id": "examples-ex1",
      "language": "c",
      "code": "reply = redisCommand(c, \"HSET myhash field1 foo\");\n    printf(\"%lld\\n\", reply->integer);\n    // >>> 1\n    freeReplyObject(reply);\n\n    reply = redisCommand(c, \"HGET myhash field1\");\n    printf(\"%s\\n\", reply->str);\n    // >>> foo\n    freeReplyObject(reply);\n\n    // A field that does not exist gives a nil reply, not an empty string.\n    reply = redisCommand(c, \"HGET myhash field2\");\n    printf(\"%s\\n\", reply->type == REDIS_REPLY_NIL ? \"(nil)\" : reply->str);\n    // >>> (nil)\n    freeReplyObject(reply);",
      "section_id": "examples"
    },
    {
      "id": "examples-ex2",
      "language": "csharp",
      "code": "bool hgetRes1 = db.HashSet(\"myhash\", \"field1\", \"foo\");\n\n        RedisValue hgetRes2 = db.HashGet(\"myhash\", \"field1\");\n        Console.WriteLine(hgetRes2);    // >>> foo\n\n        RedisValue hgetRes3 = db.HashGet(\"myhash\", \"field2\");\n        Console.WriteLine(hgetRes3);    // >>> Null",
      "section_id": "examples"
    },
    {
      "id": "examples-ex3",
      "language": "go",
      "code": "res7, err := rdb.HSet(ctx, \"myhash\", \"field1\", \"foo\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res7) // >>> 1\n\n\tres8, err := rdb.HGet(ctx, \"myhash\", \"field1\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res8) // >>> foo\n\n\tres9, err := rdb.HGet(ctx, \"myhash\", \"field2\").Result()\n\n\tif err != nil {\n\t\tfmt.Println(err)\n\t}\n\n\tfmt.Println(res9) // >>> <empty string>",
      "section_id": "examples"
    },
    {
      "id": "examples-ex4",
      "language": "java",
      "code": "// `hset` returns true because `field1` is a new field.\n            boolean res10 = syncCommands.hset(\"myhash\", \"field1\", \"foo\");\n            System.out.println(res10); // >>> true\n\n            String res11 = syncCommands.hget(\"myhash\", \"field1\");\n            System.out.println(res11); // >>> foo\n\n            // `hget` returns null because `field2` doesn't exist.\n            String res12 = syncCommands.hget(\"myhash\", \"field2\");\n            System.out.println(res12); // >>> null",
      "section_id": "examples"
    },
    {
      "id": "examples-ex5",
      "language": "java",
      "code": "Map<String, String> hGetExampleParams = new HashMap<>();\n            hGetExampleParams.put(\"field1\", \"foo\");\n\n            CompletableFuture<Void> hGetExample = asyncCommands.hset(\"myhash\", hGetExampleParams).thenCompose(res1 -> {\n                System.out.println(res1); // >>> 1\n                return asyncCommands.hget(\"myhash\", \"field1\");\n            }).thenCompose(res2 -> {\n                System.out.println(res2); // >>> foo\n                return asyncCommands.hget(\"myhash\", \"field2\");\n            }).thenAccept(res3 -> {\n                System.out.println(res3); // >>> null\n            }).toCompletableFuture();",
      "section_id": "examples"
    },
    {
      "id": "examples-ex6",
      "language": "java",
      "code": "Map<String, String> hGetExampleParams = new HashMap<>();\n            hGetExampleParams.put(\"field1\", \"foo\");\n\n            Mono<Long> hGetExample1 = reactiveCommands.hset(\"myhash\", hGetExampleParams).doOnNext(result -> {\n                System.out.println(result); // >>> 1\n            });\n\n            hGetExample1.block();\n\n            Mono<String> hGetExample2 = reactiveCommands.hget(\"myhash\", \"field1\").doOnNext(result -> {\n                System.out.println(result); // >>> foo\n            });\n\n            hGetExample2.block();\n\n            Mono<String> hGetExample3 = reactiveCommands.hget(\"myhash\", \"field2\").doOnNext(result -> {\n                System.out.println(result); // >>> null\n            });",
      "section_id": "examples"
    },
    {
      "id": "examples-ex7",
      "language": "java",
      "code": "Map<String, String> hGetExampleParams = new HashMap<>();\n        hGetExampleParams.put(\"field1\", \"foo\");\n\n        long hGetResult1 = jedis.hset(\"myhash\", hGetExampleParams);\n        System.out.println(hGetResult1);    // >>> 1\n\n        String hGetResult2 = jedis.hget(\"myhash\", \"field1\");\n        System.out.println(hGetResult2);    // >>> foo\n\n        String hGetResult3 = jedis.hget(\"myhash\", \"field2\");\n        System.out.println(hGetResult3);    // >>> null",
      "section_id": "examples"
    },
    {
      "id": "examples-ex8",
      "language": "javascript",
      "code": "const res7 = await client.hSet('myhash', 'field1', 'foo')\nconsole.log(res7) // 1\n\nconst res8 = await client.hGet('myhash', 'field1')\nconsole.log(res8) // foo\n\nconst res9 = await client.hGet('myhash', 'field2')\nconsole.log(res9) // null",
      "section_id": "examples"
    },
    {
      "id": "examples-ex9",
      "language": "javascript",
      "code": "const hgetRes1 = await redis.hset('myhash', 'field1', 'foo');\nconsole.log(hgetRes1); // >>> 1\n\nconst hgetRes2 = await redis.hget('myhash', 'field1');\nconsole.log(hgetRes2); // >>> foo\n\n// A field that does not exist reads back as null.\nconst hgetRes3 = await redis.hget('myhash', 'field2');\nconsole.log(hgetRes3); // >>> null",
      "section_id": "examples"
    },
    {
      "id": "examples-ex10",
      "language": "javascript",
      "code": "const res7 = await client.hSet('myhash', 'field1', 'foo')\nconsole.log(res7) // 1\n\nconst res8 = await client.hGet('myhash', 'field1')\nconsole.log(res8) // foo\n\nconst res9 = await client.hGet('myhash', 'field2')\nconsole.log(res9) // null",
      "section_id": "examples"
    },
    {
      "id": "examples-ex11",
      "language": "javascript",
      "code": "const hgetRes1 = await redis.hset('myhash', 'field1', 'foo');\nconsole.log(hgetRes1); // >>> 1\n\nconst hgetRes2 = await redis.hget('myhash', 'field1');\nconsole.log(hgetRes2); // >>> foo\n\n// A field that does not exist reads back as null.\nconst hgetRes3 = await redis.hget('myhash', 'field2');\nconsole.log(hgetRes3); // >>> null",
      "section_id": "examples"
    },
    {
      "id": "examples-ex12",
      "language": "php",
      "code": "$hGetResult1 = $this->redis->hset('myhash', 'field1', 'foo');\n        echo \"HSET myhash field1 foo: \" . $hGetResult1 . \"\\n\"; // >>> 1\n\n        $hGetResult2 = $this->redis->hget('myhash', 'field1');\n        echo \"HGET myhash field1: \" . ($hGetResult2 ?? 'null') . \"\\n\"; // >>> foo\n\n        $hGetResult3 = $this->redis->hget('myhash', 'field2');\n        echo \"HGET myhash field2: \" . ($hGetResult3 ?? 'null') . \"\\n\"; // >>> null",
      "section_id": "examples"
    },
    {
      "id": "examples-ex13",
      "language": "python",
      "code": "res7 = r.hset(\"myhash\", \"field1\", \"foo\")\nprint(res7)\n# >>> 1\n\nres8 = r.hget(\"myhash\", \"field1\")\nprint(res8)\n# >>> foo\n\nres9 = r.hget(\"myhash\", \"field2\")\nprint(res9)\n# >>> None",
      "section_id": "examples"
    },
    {
      "id": "examples-ex14",
      "language": "ruby",
      "code": "res10 = r.hset('myhash', 'field1', 'foo')\nputs res10 # >>> 1\n\nres11 = r.hget('myhash', 'field1')\nputs res11 # >>> foo\n\nres12 = r.hget('myhash', 'field2')\nputs res12.inspect # >>> nil",
      "section_id": "examples"
    },
    {
      "id": "examples-ex15",
      "language": "rust",
      "code": "match r.hset(\"myhash\", \"field1\", \"foo\").await {\n            Ok(res7) => {\n                let res7: i32 = res7;\n                println!(\"{res7}\");    // >>> 1\n            },\n            Err(e) => {\n                println!(\"Error setting hash field: {e}\");\n                return;\n            }\n        }\n\n        match r.hget(\"myhash\", \"field1\").await {\n            Ok(res8) => {\n                let res8: Option<String> = res8;\n                match res8 {\n                    Some(value) => {\n                        println!(\"{value}\");    // >>> foo\n                    },\n                    None => {\n                        println!(\"None\");\n                    }\n                }\n            },\n            Err(e) => {\n                println!(\"Error getting hash field: {e}\");\n                return;\n            }\n        }\n\n        match r.hget(\"myhash\", \"field2\").await {\n            Ok(res9) => {\n                let res9: Option<String> = res9;\n                match res9 {\n                    Some(_) => {\n                        println!(\"Some value\");\n                    },\n                    None => {\n                        println!(\"None\");    // >>> None\n                    }\n                }\n            },\n            Err(e) => {\n                println!(\"Error getting hash field: {e}\");\n                return;\n            }\n        }",
      "section_id": "examples"
    },
    {
      "id": "examples-ex16",
      "language": "rust",
      "code": "match r.hset(\"myhash\", \"field1\", \"foo\") {\n            Ok(res7) => {\n                let res7: i32 = res7;\n                println!(\"{res7}\");    // >>> 1\n            },\n            Err(e) => {\n                println!(\"Error setting hash field: {e}\");\n                return;\n            }\n        }\n\n        match r.hget(\"myhash\", \"field1\") {\n            Ok(res8) => {\n                let res8: Option<String> = res8;\n                match res8 {\n                    Some(value) => {\n                        println!(\"{value}\");    // >>> foo\n                    },\n                    None => {\n                        println!(\"None\");\n                    }\n                }\n            },\n            Err(e) => {\n                println!(\"Error getting hash field: {e}\");\n                return;\n            }\n        }\n\n        match r.hget(\"myhash\", \"field2\") {\n            Ok(res9) => {\n                let res9: Option<String> = res9;\n                match res9 {\n                    Some(_) => {\n                        println!(\"Some value\");\n                    },\n                    None => {\n                        println!(\"None\");    // >>> None\n                    }\n                }\n            },\n            Err(e) => {\n                println!(\"Error getting hash field: {e}\");\n                return;\n            }\n        }",
      "section_id": "examples"
    }
  ]
}
