{
  "schema_version": 3,
  "id": "integrate/spring-framework-cache/json",
  "title": "Use JSON documents with Spring Data Redis",
  "url": "https://redis.io/docs/latest/integrate/spring-framework-cache/json/",
  "summary": "Store, retrieve, and update JSON documents from a Spring Data Redis application.",
  "tags": [
    "docs",
    "integrate",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "client"
  ],
  "last_updated": "2026-11-30T15:25:33Z",
  "page_type": "content",
  "content_hash": "379852192afbe6aae9014c99d6b176476024ec460085036d75bd08cf157e569a",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Spring Data Redis lets you work with\n[JSON](https://redis.io/docs/latest/develop/data-types/json) documents through a\ntemplate-based, fluent API. This builds on the underlying\n[Lettuce](https://redis.io/docs/latest/develop/clients/lettuce) and\n[Jedis](https://redis.io/docs/latest/develop/clients/jedis) clients, so your Spring\napplication can store, retrieve, and update JSON documents without dropping\ndown to the low-level command API.\n\nThe JSON API serializes your own Java objects to and from JSON using a\ndedicated serializer, and supports [JSON path](https://redis.io/docs/latest/develop/data-types/json/path)\nexpressions so you can read and update parts of a document without\ntransferring the whole thing."
    },
    {
      "id": "requirements",
      "title": "Requirements",
      "role": "content",
      "text": "To use the JSON support, you need:\n\n- A Redis server with the JSON capability, such as\n  [Redis Open Source](https://redis.io/docs/latest/operate/oss_and_stack/) 8 or later,\n  or [Redis Stack](https://redis.io/docs/latest/operate/oss_and_stack/install/install-stack/).\n- The Lettuce or Jedis client on your classpath (Spring Data Redis works with\n  either).\n- A JSON library. The examples below use\n  [Jackson](https://github.com/FasterXML/jackson), which Spring Data Redis\n  uses by default for JSON serialization."
    },
    {
      "id": "set-up",
      "title": "Set up",
      "role": "content",
      "text": "Add Spring Data Redis to your build. For Maven, edit your `pom.xml`:\n\n[code example]\n\nFor Gradle, add the following to your `build.gradle`:\n\n[code example]\n\nConfigure a `RedisJsonTemplate` bean. The template takes its connection\nfactory, a key serializer, and a JSON serializer as constructor arguments.\nIts single type parameter is the *key* type, not the document type:\n\n[code example]\n\nThe JSON serializer is not bound to a document type. Pass\n`GenericJacksonJsonRedisSerializer` for Jackson 3 or\n`GenericJackson2JsonRedisSerializer` for Jackson 2; you choose the Java type\nwhen you read a value back.\n\nThe examples below use a simple `User` type:\n\n[code example]"
    },
    {
      "id": "store-and-retrieve-a-document",
      "title": "Store and retrieve a document",
      "role": "content",
      "text": "Use the `value()` spec to work with a whole document. Call `set()` to store an\nobject as a JSON document, and `get()` to read it back:\n\n[code example]"
    },
    {
      "id": "work-with-json-paths",
      "title": "Work with JSON paths",
      "role": "content",
      "text": "Pass a [JSON path](https://redis.io/docs/latest/develop/data-types/json/path) to `path()`\nto target part of a document instead of the whole thing. This lets you read or\nupdate a single field without rewriting the entire object.\n\n[code example]"
    },
    {
      "id": "update-arrays",
      "title": "Update arrays",
      "role": "content",
      "text": "Use the `array()` spec for array fields. You can append elements, read the\ncurrent length, trim the array to a range, or find the index of a value:\n\n[code example]"
    },
    {
      "id": "update-strings",
      "title": "Update strings",
      "role": "content",
      "text": "Use the `string()` spec to append to string fields and read their length:\n\n[code example]"
    },
    {
      "id": "toggle-booleans",
      "title": "Toggle booleans",
      "role": "content",
      "text": "Use the `bool()` spec to flip boolean fields between `true` and `false`:\n\n[code example]"
    },
    {
      "id": "merge-into-a-document",
      "title": "Merge into a document",
      "role": "content",
      "text": "Use `mergeWith()` to merge new data into an existing document, following the\n[JSON merge](https://redis.io/docs/latest/commands/json.merge) semantics. Fields in the\nsupplied object are added or overwritten, and setting a field to `null`\nremoves it:\n\n[code example]"
    },
    {
      "id": "further-reading",
      "title": "Further reading",
      "role": "content",
      "text": "- [Redis 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- [Spring Data Redis reference documentation](https://docs.spring.io/spring-data/redis/reference/)"
    }
  ],
  "examples": [
    {
      "id": "set-up-ex0",
      "language": "xml",
      "code": "<dependency>\n    <groupId>org.springframework.data</groupId>\n    <artifactId>spring-data-redis</artifactId>\n    <version>4.2.0</version>\n</dependency>",
      "section_id": "set-up"
    },
    {
      "id": "set-up-ex1",
      "language": "groovy",
      "code": "implementation 'org.springframework.data:spring-data-redis:4.2.0'",
      "section_id": "set-up"
    },
    {
      "id": "set-up-ex2",
      "language": "java",
      "code": "@Configuration\npublic class RedisJsonConfig {\n\n    @Bean\n    public RedisJsonTemplate<String> redisJsonTemplate(\n            RedisConnectionFactory connectionFactory) {\n\n        return new RedisJsonTemplate<>(connectionFactory,\n                RedisSerializer.string(),\n                GenericJacksonJsonRedisSerializer.builder().build());\n    }\n}",
      "section_id": "set-up"
    },
    {
      "id": "set-up-ex3",
      "language": "java",
      "code": "public class User {\n    private String name;\n    private int age;\n    private String city;\n    private List<String> interests;\n\n    // Constructors, getters, and setters omitted.\n}",
      "section_id": "set-up"
    },
    {
      "id": "store-and-retrieve-a-document-ex0",
      "language": "java",
      "code": "User user = new User(\"Paul\", 42, \"London\", List.of(\"golf\", \"coding\"));\n\n// Store the object as a JSON document at the key \"user:1\".\njsonTemplate.value(\"user:1\").set(user);\n\n// Read the whole document back as a User object.\nJsonResult result = jsonTemplate.value(\"user:1\").get();\nUser stored = result.as(User.class);",
      "section_id": "store-and-retrieve-a-document"
    },
    {
      "id": "work-with-json-paths-ex0",
      "language": "java",
      "code": "// Update just the \"city\" field.\njsonTemplate.value(\"user:1\").path(\"$.city\").set(\"Manchester\");\n\n// Set a field only if it does not already exist.\njsonTemplate.value(\"user:1\").path(\"$.nickname\").setIfAbsent(\"Paulie\");\n\n// Set a field only if it already exists.\njsonTemplate.value(\"user:1\").path(\"$.city\").setIfPresent(\"Bristol\");\n\n// Read a specific path.\nString city = jsonTemplate.value(\"user:1\").path(\"$.city\").get().as(String.class);",
      "section_id": "work-with-json-paths"
    },
    {
      "id": "update-arrays-ex0",
      "language": "java",
      "code": "// Append a value to the \"interests\" array.\njsonTemplate.array(\"user:1\").path(\"$.interests\").append(\"cycling\");\n\n// Get the length of the array.\nList<Long> lengths = jsonTemplate.array(\"user:1\").path(\"$.interests\").length();\n\n// Find the index of a value.\nList<Long> index = jsonTemplate.array(\"user:1\").path(\"$.interests\").indexOf(\"golf\");\n\n// Keep only the first two elements.\njsonTemplate.array(\"user:1\").path(\"$.interests\").trim(0, 1);",
      "section_id": "update-arrays"
    },
    {
      "id": "update-strings-ex0",
      "language": "java",
      "code": "// Append to a string field.\njsonTemplate.string(\"user:1\").path(\"$.name\").append(\" Jones\");\n\n// Get the length of the string.\nList<Long> nameLength = jsonTemplate.string(\"user:1\").path(\"$.name\").length();",
      "section_id": "update-strings"
    },
    {
      "id": "toggle-booleans-ex0",
      "language": "java",
      "code": "// Flip the \"active\" flag.\nList<Boolean> newValues = jsonTemplate.bool(\"user:1\").path(\"$.active\").toggle();",
      "section_id": "toggle-booleans"
    },
    {
      "id": "merge-into-a-document-ex0",
      "language": "java",
      "code": "// Merge in a partial object to update several fields at once.\nMap<String, Object> changes = Map.of(\"city\", \"Leeds\", \"age\", 43);\njsonTemplate.value(\"user:1\").mergeWith(changes);",
      "section_id": "merge-into-a-document"
    }
  ]
}
