{
  "id": "json",
  "title": "Work with JSON documents",
  "url": "https://redis.io/docs/latest/develop/clients/rust/json/",
  "summary": "Learn how to store, read, and update JSON documents with redis-rs.",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-05-18T05:44:54-07:00",
  "page_type": "content",
  "content_hash": "cdf30a87ee96d9cb147818b803726443c95abc237fff7b1e267b86bd2260dcfc",
  "sections": [
    {
      "id": "install",
      "title": "Install",
      "role": "setup",
      "text": "Add `serde_json` and enable the `json` feature for `redis`.\nIf you want to use the async API, also enable a runtime integration such as `tokio-comp`.\n\n[code example]"
    },
    {
      "id": "import-the-required-crates",
      "title": "Import the required crates",
      "role": "content",
      "text": "The example uses `serde_json::json!()` to build the document and the\n`JsonCommands` or `JsonAsyncCommands` trait to access Redis JSON commands.\n\nFoundational: Import the Redis JSON traits and serde_json helpers needed to work with JSON documents in Rust\n\n**Difficulty:** Beginner\n\n**Available in:** Rust (Asynchronous), Rust (Synchronous)\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]"
    },
    {
      "id": "create-some-json-data",
      "title": "Create some JSON data",
      "role": "content",
      "text": "Create a sample document representing a bike listing.\nThe nested `specs` and `inventory` objects and the `colors` array let you see how\nJSON paths work with more realistic data than a flat object.\n\nFoundational: Define a nested JSON document with objects and arrays using serde_json::json!\n\n**Difficulty:** Beginner\n\n**Available in:** Rust (Asynchronous), Rust (Synchronous)\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]"
    },
    {
      "id": "connect-to-redis",
      "title": "Connect to Redis",
      "role": "content",
      "text": "Connect to your Redis server in the usual way.\nSee [Connect to the server](https://redis.io/docs/latest/develop/clients/rust)\nfor more connection options.\n\nFoundational: Create a Redis client and open a sync or async connection from Rust\n\n**Difficulty:** Beginner\n\n**Available in:** Rust (Asynchronous), Rust (Synchronous)\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]"
    },
    {
      "id": "store-and-retrieve-the-document",
      "title": "Store and retrieve the document",
      "role": "content",
      "text": "Use [`JSON.SET`](https://redis.io/docs/latest/commands/json.set) to store the whole document at the root path `$`.\nYou can then retrieve the document again with [`JSON.GET`](https://redis.io/docs/latest/commands/json.get).\n\nFoundational: Store a complete JSON document with JSON.SET and fetch it again with JSON.GET\n\n**Difficulty:** Beginner\n\n**Available in:** Rust (Asynchronous), Rust (Synchronous)\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]"
    },
    {
      "id": "read-nested-fields",
      "title": "Read nested fields",
      "role": "content",
      "text": "JSON paths let you retrieve only the parts of the document you need.\nThis is useful when your application only needs a small subset of the data.\n\nRead nested data: Use JSON paths to retrieve selected fields and arrays without fetching the whole document\n\n**Difficulty:** Beginner\n\n**Available in:** Rust (Asynchronous), Rust (Synchronous)\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]"
    },
    {
      "id": "update-part-of-the-document",
      "title": "Update part of the document",
      "role": "content",
      "text": "You can update individual fields without replacing the whole document.\nThe example below changes the stock count and then applies a price change with\n[`JSON.NUMINCRBY`](https://redis.io/docs/latest/commands/json.numincrby).\n\nUpdate nested values: Modify individual fields in place with JSON.SET and JSON.NUMINCRBY\n\n**Difficulty:** Intermediate\n\n**Available in:** Rust (Asynchronous), Rust (Synchronous)\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]"
    },
    {
      "id": "append-to-an-array",
      "title": "Append to an array",
      "role": "content",
      "text": "You can also update arrays in place.\nThis example adds another color to the bike and then retrieves the updated array.\n\nUpdate arrays: Append new elements to a JSON array and read back the updated value\n\n**Difficulty:** Intermediate\n\n**Available in:** Rust (Asynchronous), Rust (Synchronous)\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]"
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "See the following pages to learn more:\n\n- [JSON data type](https://redis.io/docs/latest/develop/data-types/json)\n- [JSON path syntax](https://redis.io/docs/latest/develop/data-types/json/path)\n- [`redis-rs` documentation](https://docs.rs/redis/latest/redis/)"
    }
  ],
  "examples": [
    {
      "id": "install-ex0",
      "language": "toml",
      "code": "[dependencies]\nserde_json = \"1\"\n\n# Sync API\nredis = { version = \"1.0.4\", features = [\"json\"] }\n\n# Async API with Tokio\ntokio = { version = \"1\", features = [\"full\"] }\nredis = { version = \"1.0.4\", features = [\"json\", \"tokio-comp\"] }",
      "section_id": "install"
    },
    {
      "id": "import-the-required-crates-ex0",
      "language": "rust",
      "code": "use redis::{cmd, AsyncCommands, JsonAsyncCommands};\n    use serde_json::json;",
      "section_id": "import-the-required-crates"
    },
    {
      "id": "import-the-required-crates-ex1",
      "language": "rust",
      "code": "use redis::{cmd, Commands, JsonCommands};\n    use serde_json::json;",
      "section_id": "import-the-required-crates"
    },
    {
      "id": "create-some-json-data-ex0",
      "language": "rust",
      "code": "let bike = json!({\n            \"model\": \"Deimos\",\n            \"brand\": \"Ergonom\",\n            \"price\": 4972,\n            \"specs\": {\n                \"material\": \"carbon\",\n                \"weight\": 8.7\n            },\n            \"colors\": [\"black\", \"silver\"],\n            \"inventory\": {\n                \"in_stock\": 12,\n                \"warehouse\": \"w1\"\n            }\n        });",
      "section_id": "create-some-json-data"
    },
    {
      "id": "create-some-json-data-ex1",
      "language": "rust",
      "code": "let bike = json!({\n            \"model\": \"Deimos\",\n            \"brand\": \"Ergonom\",\n            \"price\": 4972,\n            \"specs\": {\n                \"material\": \"carbon\",\n                \"weight\": 8.7\n            },\n            \"colors\": [\"black\", \"silver\"],\n            \"inventory\": {\n                \"in_stock\": 12,\n                \"warehouse\": \"w1\"\n            }\n        });",
      "section_id": "create-some-json-data"
    },
    {
      "id": "connect-to-redis-ex0",
      "language": "rust",
      "code": "let client =\n            redis::Client::open(\"redis://127.0.0.1\").expect(\"Failed to create Redis client\");\n        let mut r = client\n            .get_multiplexed_async_connection()\n            .await\n            .expect(\"Failed to connect to Redis\");",
      "section_id": "connect-to-redis"
    },
    {
      "id": "connect-to-redis-ex1",
      "language": "rust",
      "code": "let client =\n            redis::Client::open(\"redis://127.0.0.1\").expect(\"Failed to create Redis client\");\n        let mut r = client.get_connection().expect(\"Failed to connect to Redis\");",
      "section_id": "connect-to-redis"
    },
    {
      "id": "store-and-retrieve-the-document-ex0",
      "language": "rust",
      "code": "let stored: bool = r\n            .json_set(\"bike:1\", \"$\", &bike)\n            .await\n            .expect(\"Failed to run JSON.SET\");\n        println!(\"{}\", if stored { \"OK\" } else { \"(nil)\" }); // >>> OK\n\n        let bike_json: String = r\n            .json_get(\"bike:1\", \"$\")\n            .await\n            .expect(\"Failed to run JSON.GET\");\n        println!(\"{bike_json}\");\n        // >>> [{\"model\":\"Deimos\",\"brand\":\"Ergonom\",\"price\":4972,\"specs\":{\"material\":\"carbon\",\"weight\":8.7},\"colors\":[\"black\",\"silver\"],\"inventory\":{\"in_stock\":12,\"warehouse\":\"w1\"}}]",
      "section_id": "store-and-retrieve-the-document"
    },
    {
      "id": "store-and-retrieve-the-document-ex1",
      "language": "rust",
      "code": "let stored: bool = r\n            .json_set(\"bike:1\", \"$\", &bike)\n            .expect(\"Failed to run JSON.SET\");\n        println!(\"{}\", if stored { \"OK\" } else { \"(nil)\" }); // >>> OK\n\n        let bike_json: String = r.json_get(\"bike:1\", \"$\").expect(\"Failed to run JSON.GET\");\n        println!(\"{bike_json}\");\n        // >>> [{\"model\":\"Deimos\",\"brand\":\"Ergonom\",\"price\":4972,\"specs\":{\"material\":\"carbon\",\"weight\":8.7},\"colors\":[\"black\",\"silver\"],\"inventory\":{\"in_stock\":12,\"warehouse\":\"w1\"}}]",
      "section_id": "store-and-retrieve-the-document"
    },
    {
      "id": "read-nested-fields-ex0",
      "language": "rust",
      "code": "let material: String = r\n            .json_get(\"bike:1\", \"$.specs.material\")\n            .await\n            .expect(\"Failed to run JSON.GET\");\n        println!(\"{material}\"); // >>> [\"carbon\"]\n\n        let colors: String = r\n            .json_get(\"bike:1\", \"$.colors\")\n            .await\n            .expect(\"Failed to run JSON.GET\");\n        println!(\"{colors}\"); // >>> [[\"black\",\"silver\"]]\n\n        let stock: String = r\n            .json_get(\"bike:1\", \"$.inventory.in_stock\")\n            .await\n            .expect(\"Failed to run JSON.GET\");\n        println!(\"{stock}\"); // >>> [12]",
      "section_id": "read-nested-fields"
    },
    {
      "id": "read-nested-fields-ex1",
      "language": "rust",
      "code": "let material: String = r\n            .json_get(\"bike:1\", \"$.specs.material\")\n            .expect(\"Failed to run JSON.GET\");\n        println!(\"{material}\"); // >>> [\"carbon\"]\n\n        let colors: String = r\n            .json_get(\"bike:1\", \"$.colors\")\n            .expect(\"Failed to run JSON.GET\");\n        println!(\"{colors}\"); // >>> [[\"black\",\"silver\"]]\n\n        let stock: String = r\n            .json_get(\"bike:1\", \"$.inventory.in_stock\")\n            .expect(\"Failed to run JSON.GET\");\n        println!(\"{stock}\"); // >>> [12]",
      "section_id": "read-nested-fields"
    },
    {
      "id": "update-part-of-the-document-ex0",
      "language": "rust",
      "code": "let stock_set: bool = r\n            .json_set(\"bike:1\", \"$.inventory.in_stock\", &json!(8))\n            .await\n            .expect(\"Failed to update stock\");\n        println!(\"{}\", if stock_set { \"OK\" } else { \"(nil)\" }); // >>> OK\n\n        let new_price: String = cmd(\"JSON.NUMINCRBY\")\n            .arg(\"bike:1\")\n            .arg(\"$.price\")\n            .arg(-500)\n            .query_async(&mut r)\n            .await\n            .expect(\"Failed to run JSON.NUMINCRBY\");\n        println!(\"{new_price}\"); // >>> [4472]\n\n        let updated_fields: String = r\n            .json_get(\"bike:1\", &[\"$.price\", \"$.inventory.in_stock\"])\n            .await\n            .expect(\"Failed to read updated fields\");\n        println!(\"{updated_fields}\"); // >>> {\"$.price\":[4472],\"$.inventory.in_stock\":[8]}",
      "section_id": "update-part-of-the-document"
    },
    {
      "id": "update-part-of-the-document-ex1",
      "language": "rust",
      "code": "let stock_set: bool = r\n            .json_set(\"bike:1\", \"$.inventory.in_stock\", &json!(8))\n            .expect(\"Failed to update stock\");\n        println!(\"{}\", if stock_set { \"OK\" } else { \"(nil)\" }); // >>> OK\n\n        let new_price: String = cmd(\"JSON.NUMINCRBY\")\n            .arg(\"bike:1\")\n            .arg(\"$.price\")\n            .arg(-500)\n            .query(&mut r)\n            .expect(\"Failed to run JSON.NUMINCRBY\");\n        println!(\"{new_price}\"); // >>> [4472]\n\n        let updated_fields: String = r\n            .json_get(\"bike:1\", &[\"$.price\", \"$.inventory.in_stock\"])\n            .expect(\"Failed to read updated fields\");\n        println!(\"{updated_fields}\"); // >>> {\"$.price\":[4472],\"$.inventory.in_stock\":[8]}",
      "section_id": "update-part-of-the-document"
    },
    {
      "id": "append-to-an-array-ex0",
      "language": "rust",
      "code": "let _: redis::Value = cmd(\"JSON.ARRAPPEND\")\n            .arg(\"bike:1\")\n            .arg(\"$.colors\")\n            .arg(\"\\\"red\\\"\")\n            .query_async(&mut r)\n            .await\n            .expect(\"Failed to run JSON.ARRAPPEND\");\n\n        let updated_colors: String = r\n            .json_get(\"bike:1\", \"$.colors\")\n            .await\n            .expect(\"Failed to read updated colors\");\n        println!(\"{updated_colors}\"); // >>> [[\"black\",\"silver\",\"red\"]]",
      "section_id": "append-to-an-array"
    },
    {
      "id": "append-to-an-array-ex1",
      "language": "rust",
      "code": "let _: redis::Value = cmd(\"JSON.ARRAPPEND\")\n            .arg(\"bike:1\")\n            .arg(\"$.colors\")\n            .arg(\"\\\"red\\\"\")\n            .query(&mut r)\n            .expect(\"Failed to run JSON.ARRAPPEND\");\n\n        let updated_colors: String = r\n            .json_get(\"bike:1\", \"$.colors\")\n            .expect(\"Failed to read updated colors\");\n        println!(\"{updated_colors}\"); // >>> [[\"black\",\"silver\",\"red\"]]",
      "section_id": "append-to-an-array"
    }
  ]
}
