{
  "id": "hash_vs_json",
  "title": "Choose a Storage Type",
  "url": "https://redis.io/docs/latest/develop/ai/redisvl/user_guide/how_to_guides/hash_vs_json/",
  "summary": "",
  "tags": [],
  "last_updated": "2026-05-06T11:49:45+02:00",
  "page_type": "content",
  "content_hash": "8d580f44d67d4154d729b73ba7bfb2f0ee1bfec5bbbbab588210e82c4b920f26",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Redis provides a [variety of data structures](https://redis.com/redis-enterprise/data-structures/) that can adapt to your domain-specific applications. This guide demonstrates how to use RedisVL with both [Hash](https://redis.io/docs/latest/develop/data-types/#hashes) and [JSON](https://redis.io/docs/latest/develop/data-types/json/) storage types, helping you choose the right approach for your use case."
    },
    {
      "id": "prerequisites",
      "title": "Prerequisites",
      "role": "content",
      "text": "Before you begin, ensure you have:\n- Installed RedisVL: `pip install redisvl`\n- A running Redis instance ([Redis 8+](https://redis.io/downloads/) or [Redis Cloud](https://redis.io/cloud))"
    },
    {
      "id": "what-you-ll-learn",
      "title": "What You'll Learn",
      "role": "content",
      "text": "By the end of this guide, you will be able to:\n- Understand the differences between Hash and JSON storage types\n- Define schemas for both Hash and JSON storage\n- Load and query data using each storage type\n- Access nested JSON fields using JSONPath expressions\n- Choose the right storage type for your application\n\n\n[code example]\n\n\n[code example]\n\n\n<table><tr><th>user</th><th>age</th><th>job</th><th>credit_score</th><th>office_location</th><th>user_embedding</th><th>last_updated</th></tr><tr><td>john</td><td>18</td><td>engineer</td><td>high</td><td>-122.4194,37.7749</td><td>b'\\xcd\\xcc\\xcc=\\xcd\\xcc\\xcc=\\x00\\x00\\x00?'</td><td>1741627789</td></tr><tr><td>derrick</td><td>14</td><td>doctor</td><td>low</td><td>-122.4194,37.7749</td><td>b'\\xcd\\xcc\\xcc=\\xcd\\xcc\\xcc=\\x00\\x00\\x00?'</td><td>1741627789</td></tr><tr><td>nancy</td><td>94</td><td>doctor</td><td>high</td><td>-122.4194,37.7749</td><td>b'333?\\xcd\\xcc\\xcc=\\x00\\x00\\x00?'</td><td>1710696589</td></tr><tr><td>tyler</td><td>100</td><td>engineer</td><td>high</td><td>-122.0839,37.3861</td><td>b'\\xcd\\xcc\\xcc=\\xcd\\xcc\\xcc>\\x00\\x00\\x00?'</td><td>1742232589</td></tr><tr><td>tim</td><td>12</td><td>dermatologist</td><td>high</td><td>-122.0839,37.3861</td><td>b'\\xcd\\xcc\\xcc>\\xcd\\xcc\\xcc>\\x00\\x00\\x00?'</td><td>1739644189</td></tr><tr><td>taimur</td><td>15</td><td>CEO</td><td>low</td><td>-122.0839,37.3861</td><td>b'\\x9a\\x99\\x19?\\xcd\\xcc\\xcc=\\x00\\x00\\x00?'</td><td>1742232589</td></tr><tr><td>joe</td><td>35</td><td>dentist</td><td>medium</td><td>-122.0839,37.3861</td><td>b'fff?fff?\\xcd\\xcc\\xcc='</td><td>1742232589</td></tr></table>"
    },
    {
      "id": "hash-or-json-how-to-choose",
      "title": "Hash or JSON: How to Choose",
      "role": "content",
      "text": "Both storage options offer different features and tradeoffs. This section walks through a sample dataset to illustrate when and how to use each option."
    },
    {
      "id": "working-with-hashes",
      "title": "Working with Hashes",
      "role": "content",
      "text": "Hashes in Redis are simple collections of field-value pairs. Think of it like a mutable single-level dictionary contains multiple \"rows\":\n\n\n[code example]\n\nHashes are best suited for use cases with the following characteristics:\n- Performance (speed) and storage space (memory consumption) are top concerns\n- Data can be easily normalized and modeled as a single-level dict\n\nHashes are typically the default recommendation.\n\n\n[code example]\n\n\n[code example]\n\n\n[code example]\n\n\n\n\n    <StorageType.HASH: 'hash'>\n\n\n\n#### Vectors as byte strings\nOne nuance when working with Hashes in Redis, is that all vectorized data must be passed as a byte string (for efficient storage, indexing, and processing). An example of that can be seen below:\n\n\n[code example]\n\n\n\n\n    {'user': 'john',\n     'age': 18,\n     'job': 'engineer',\n     'credit_score': 'high',\n     'office_location': '-122.4194,37.7749',\n     'user_embedding': b'\\xcd\\xcc\\xcc=\\xcd\\xcc\\xcc=\\x00\\x00\\x00?',\n     'last_updated': 1741627789}\n\n\n\n\n[code example]\n\n\n[code example]\n\n    \n    Statistics:\n    ╭─────────────────────────────┬────────────╮\n    │ Stat Key                    │ Value      │\n    ├─────────────────────────────┼────────────┤\n    │ num_docs                    │ 7          │\n    │ num_terms                   │ 6          │\n    │ max_doc_id                  │ 7          │\n    │ num_records                 │ 44         │\n    │ percent_indexed             │ 1          │\n    │ hash_indexing_failures      │ 0          │\n    │ number_of_uses              │ 1          │\n    │ bytes_per_record_avg        │ 39.0681800 │\n    │ doc_table_size_mb           │ 0.00837230 │\n    │ inverted_sz_mb              │ 0.00163936 │\n    │ key_table_size_mb           │ 3.50952148 │\n    │ offset_bits_per_record_avg  │ 8          │\n    │ offset_vectors_sz_mb        │ 8.58306884 │\n    │ offsets_per_term_avg        │ 0.20454545 │\n    │ records_per_doc_avg         │ 6.28571414 │\n    │ sortable_values_size_mb     │ 0          │\n    │ total_indexing_time         │ 0.55204    │\n    │ total_inverted_index_blocks │ 18         │\n    │ vector_index_sz_mb          │ 0.02820587 │\n    ╰─────────────────────────────┴────────────╯\n\n\n#### Performing Queries\nOnce our index is created and data is loaded into the right format, we can run queries against the index with RedisVL:\n\n\n[code example]\n\n\n<table><tr><th>vector_distance</th><th>user</th><th>credit_score</th><th>age</th><th>job</th><th>office_location</th></tr><tr><td>0</td><td>john</td><td>high</td><td>18</td><td>engineer</td><td>-122.4194,37.7749</td></tr><tr><td>0.109129190445</td><td>tyler</td><td>high</td><td>100</td><td>engineer</td><td>-122.0839,37.3861</td></tr></table>\n\n\n\n[code example]"
    },
    {
      "id": "working-with-json",
      "title": "Working with JSON",
      "role": "content",
      "text": "JSON is best suited for use cases with the following characteristics:\n- Ease of use and data model flexibility are top concerns\n- Application data is already native JSON\n- Replacing another document storage/db solution\n\n\n[code example]\n\n\n[code example]\n\n#### Vectors as Float Arrays\nVectorized data stored in JSON must be a pure array (Python list) of floats. The following code modifies the sample data to use this format:\n\n\n[code example]\n\n\n[code example]\n\n\n\n\n    {'user': 'john',\n     'age': 18,\n     'job': 'engineer',\n     'credit_score': 'high',\n     'office_location': '-122.4194,37.7749',\n     'user_embedding': [0.10000000149011612, 0.10000000149011612, 0.5],\n     'last_updated': 1741627789}\n\n\n\n\n[code example]\n\n\n[code example]\n\n\n<table><tr><th>vector_distance</th><th>user</th><th>credit_score</th><th>age</th><th>job</th><th>office_location</th></tr><tr><td>0</td><td>john</td><td>high</td><td>18</td><td>engineer</td><td>-122.4194,37.7749</td></tr><tr><td>0.109129190445</td><td>tyler</td><td>high</td><td>100</td><td>engineer</td><td>-122.0839,37.3861</td></tr></table>"
    },
    {
      "id": "cleanup",
      "title": "Cleanup",
      "role": "content",
      "text": "[code example]"
    },
    {
      "id": "working-with-nested-data-in-json",
      "title": "Working with nested data in JSON",
      "role": "content",
      "text": "Redis also supports native **JSON** objects. These can be multi-level (nested) objects, with full JSONPath support for updating/retrieving sub elements:\n\n[code example]"
    },
    {
      "id": "full-json-path-support",
      "title": "Full JSON Path support",
      "role": "content",
      "text": "Because Redis enables full JSON path support, when creating an index schema, elements need to be indexed and selected by their path with the desired `name` AND `path` that points to where the data is located within the objects.\n\nBy default, RedisVL will assume the path as `$.{name}` if not provided in JSON fields schema. If nested provide path as `$.object.attribute`"
    },
    {
      "id": "as-an-example",
      "title": "As an example:",
      "role": "content",
      "text": "[code example]\n\n\n[code example]\n\n\n[code example]\n\n\n\n\n    ['bike-json:01KHKJ5WW3DJE0X6E85GG27V0X',\n     'bike-json:01KHKJ5WW3DJE0X6E85GG27V0Y']\n\n\n\n\n[code example]\n\n**Note:** As shown in the example if you want to retrieve a field from json object that was not indexed you will also need to supply the full path as with `$.metadata.type`.\n\n\n[code example]\n\n\n\n\n    [{'id': 'bike-json:01KHKJ5WW3DJE0X6E85GG27V0Y',\n      'vector_distance': '0.519988954067',\n      'brand': 'Trek',\n      '$.metadata.type': 'Enduro bikes'},\n     {'id': 'bike-json:01KHKJ5WW3DJE0X6E85GG27V0X',\n      'vector_distance': '0.65762424469',\n      'brand': 'Specialized',\n      '$.metadata.type': 'Enduro bikes'}]"
    },
    {
      "id": "next-steps",
      "title": "Next Steps",
      "role": "content",
      "text": "Now that you understand Hash vs JSON storage, explore these related guides:\n\n- [Getting Started](https://redis.io/docs/latest/../getting_started) - Learn the basics of RedisVL indexes and queries\n- [Query and Filter Data](https://redis.io/docs/latest/complex_filtering) - Apply filters to narrow down search results\n- [Use Advanced Query Types](https://redis.io/docs/latest/advanced_queries) - Explore TextQuery, HybridQuery, and more\n\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "what-you-ll-learn-ex0",
      "language": "python",
      "code": "# import necessary modules\nimport pickle\n\nfrom redisvl.redis.utils import buffer_to_array\nfrom redisvl.index import SearchIndex\n\n\n# load in the example data and printing utils\ndata = pickle.load(open(\"hybrid_example_data.pkl\", \"rb\"))",
      "section_id": "what-you-ll-learn"
    },
    {
      "id": "what-you-ll-learn-ex1",
      "language": "python",
      "code": "from jupyterutils import result_print, table_print\n\ntable_print(data)",
      "section_id": "what-you-ll-learn"
    },
    {
      "id": "working-with-hashes-ex0",
      "language": "python",
      "code": "{\n    \"model\": \"Deimos\",\n    \"brand\": \"Ergonom\",\n    \"type\": \"Enduro bikes\",\n    \"price\": 4972,\n}",
      "section_id": "working-with-hashes"
    },
    {
      "id": "working-with-hashes-ex1",
      "language": "python",
      "code": "# define the hash index schema\nhash_schema = {\n    \"index\": {\n        \"name\": \"user-hash\",\n        \"prefix\": \"user-hash-docs\",\n        \"storage_type\": \"hash\", # default setting -- HASH\n    },\n    \"fields\": [\n        {\"name\": \"user\", \"type\": \"tag\"},\n        {\"name\": \"credit_score\", \"type\": \"tag\"},\n        {\"name\": \"job\", \"type\": \"text\"},\n        {\"name\": \"age\", \"type\": \"numeric\"},\n        {\"name\": \"office_location\", \"type\": \"geo\"},\n        {\n            \"name\": \"user_embedding\",\n            \"type\": \"vector\",\n            \"attrs\": {\n                \"dims\": 3,\n                \"distance_metric\": \"cosine\",\n                \"algorithm\": \"flat\",\n                \"datatype\": \"float32\"\n            }\n\n        }\n    ],\n}",
      "section_id": "working-with-hashes"
    },
    {
      "id": "working-with-hashes-ex2",
      "language": "python",
      "code": "# construct a search index from the hash schema\nhindex = SearchIndex.from_dict(hash_schema, redis_url=\"redis://localhost:6379\")\n\n# create the index (no data yet)\nhindex.create(overwrite=True)",
      "section_id": "working-with-hashes"
    },
    {
      "id": "working-with-hashes-ex3",
      "language": "python",
      "code": "# show the underlying storage type\nhindex.storage_type",
      "section_id": "working-with-hashes"
    },
    {
      "id": "working-with-hashes-ex4",
      "language": "python",
      "code": "# show a single entry from the data that will be loaded\ndata[0]",
      "section_id": "working-with-hashes"
    },
    {
      "id": "working-with-hashes-ex5",
      "language": "python",
      "code": "# load hash data\nkeys = hindex.load(data)",
      "section_id": "working-with-hashes"
    },
    {
      "id": "working-with-hashes-ex6",
      "language": "python",
      "code": "!rvl stats -i user-hash",
      "section_id": "working-with-hashes"
    },
    {
      "id": "working-with-hashes-ex7",
      "language": "python",
      "code": "from redisvl.query import VectorQuery\nfrom redisvl.query.filter import Tag, Text, Num\n\nt = (Tag(\"credit_score\") == \"high\") & (Text(\"job\") % \"enginee*\") & (Num(\"age\") > 17)  # codespell:ignore enginee\n\nv = VectorQuery(\n    vector=[0.1, 0.1, 0.5],\n    vector_field_name=\"user_embedding\",\n    return_fields=[\"user\", \"credit_score\", \"age\", \"job\", \"office_location\"],\n    filter_expression=t\n)\n\n\nresults = hindex.query(v)\nresult_print(results)",
      "section_id": "working-with-hashes"
    },
    {
      "id": "working-with-hashes-ex8",
      "language": "python",
      "code": "# clean up\nhindex.delete()",
      "section_id": "working-with-hashes"
    },
    {
      "id": "working-with-json-ex0",
      "language": "python",
      "code": "# define the json index schema\njson_schema = {\n    \"index\": {\n        \"name\": \"user-json\",\n        \"prefix\": \"user-json-docs\",\n        \"storage_type\": \"json\", # JSON storage type\n    },\n    \"fields\": [\n        {\"name\": \"user\", \"type\": \"tag\"},\n        {\"name\": \"credit_score\", \"type\": \"tag\"},\n        {\"name\": \"job\", \"type\": \"text\"},\n        {\"name\": \"age\", \"type\": \"numeric\"},\n        {\"name\": \"office_location\", \"type\": \"geo\"},\n        {\n            \"name\": \"user_embedding\",\n            \"type\": \"vector\",\n            \"attrs\": {\n                \"dims\": 3,\n                \"distance_metric\": \"cosine\",\n                \"algorithm\": \"flat\",\n                \"datatype\": \"float32\"\n            }\n\n        }\n    ],\n}",
      "section_id": "working-with-json"
    },
    {
      "id": "working-with-json-ex1",
      "language": "python",
      "code": "# construct a search index from the json schema\njindex = SearchIndex.from_dict(json_schema, redis_url=\"redis://localhost:6379\")\n\n# create the index (no data yet)\njindex.create(overwrite=True)",
      "section_id": "working-with-json"
    },
    {
      "id": "working-with-json-ex2",
      "language": "python",
      "code": "json_data = data.copy()\n\nfor d in json_data:\n    d['user_embedding'] = buffer_to_array(d['user_embedding'], dtype='float32')",
      "section_id": "working-with-json"
    },
    {
      "id": "working-with-json-ex3",
      "language": "python",
      "code": "# inspect a single JSON record\njson_data[0]",
      "section_id": "working-with-json"
    },
    {
      "id": "working-with-json-ex4",
      "language": "python",
      "code": "keys = jindex.load(json_data)",
      "section_id": "working-with-json"
    },
    {
      "id": "working-with-json-ex5",
      "language": "python",
      "code": "# we can now run the exact same query as above\nresult_print(jindex.query(v))",
      "section_id": "working-with-json"
    },
    {
      "id": "cleanup-ex0",
      "language": "python",
      "code": "jindex.delete()",
      "section_id": "cleanup"
    },
    {
      "id": "working-with-nested-data-in-json-ex0",
      "language": "json",
      "code": "{\n    \"name\": \"Specialized Stump jumper\",\n    \"metadata\": {\n        \"model\": \"Stumpjumper\",\n        \"brand\": \"Specialized\",\n        \"type\": \"Enduro bikes\",\n        \"price\": 3000\n    },\n}",
      "section_id": "working-with-nested-data-in-json"
    },
    {
      "id": "as-an-example-ex0",
      "language": "python",
      "code": "from redisvl.utils.vectorize import HFTextVectorizer\n\nemb_model = HFTextVectorizer()\n\nbike_data = [\n    {\n        \"name\": \"Specialized Stump jumper\",\n        \"metadata\": {\n            \"model\": \"Stumpjumper\",\n            \"brand\": \"Specialized\",\n            \"type\": \"Enduro bikes\",\n            \"price\": 3000\n        },\n        \"description\": \"The Specialized Stumpjumper is a versatile enduro bike that dominates both climbs and descents. Features a FACT 11m carbon fiber frame, FOX FLOAT suspension with 160mm travel, and SRAM X01 Eagle drivetrain. The asymmetric frame design and internal storage compartment make it a practical choice for all-day adventures.\"\n    },\n    {\n        \"name\": \"bike_2\",\n        \"metadata\": {\n            \"model\": \"Slash\",\n            \"brand\": \"Trek\",\n            \"type\": \"Enduro bikes\",\n            \"price\": 5000\n        },\n        \"description\": \"Trek's Slash is built for aggressive enduro riding and racing. Featuring Trek's Alpha Aluminum frame with RE:aktiv suspension technology, 160mm travel, and Knock Block frame protection. Equipped with Bontrager components and a Shimano XT drivetrain, this bike excels on technical trails and enduro race courses.\"\n    }\n]\n\nbike_data = [{**d, \"bike_embedding\": emb_model.embed(d[\"description\"])} for d in bike_data]\n\nbike_schema = {\n    \"index\": {\n        \"name\": \"bike-json\",\n        \"prefix\": \"bike-json\",\n        \"storage_type\": \"json\", # JSON storage type\n    },\n    \"fields\": [\n        {\n            \"name\": \"model\",\n            \"type\": \"tag\",\n            \"path\": \"$.metadata.model\" # note the '$'\n        },\n        {\n            \"name\": \"brand\",\n            \"type\": \"tag\",\n            \"path\": \"$.metadata.brand\"\n        },\n        {\n            \"name\": \"price\",\n            \"type\": \"numeric\",\n            \"path\": \"$.metadata.price\"\n        },\n        {\n            \"name\": \"bike_embedding\",\n            \"type\": \"vector\",\n            \"attrs\": {\n                \"dims\": len(bike_data[0][\"bike_embedding\"]),\n                \"distance_metric\": \"cosine\",\n                \"algorithm\": \"flat\",\n                \"datatype\": \"float32\"\n            }\n\n        }\n    ],\n}",
      "section_id": "as-an-example"
    },
    {
      "id": "as-an-example-ex1",
      "language": "python",
      "code": "# construct a search index from the json schema\nbike_index = SearchIndex.from_dict(bike_schema, redis_url=\"redis://localhost:6379\")\n\n# create the index (no data yet)\nbike_index.create(overwrite=True)",
      "section_id": "as-an-example"
    },
    {
      "id": "as-an-example-ex2",
      "language": "python",
      "code": "bike_index.load(bike_data)",
      "section_id": "as-an-example"
    },
    {
      "id": "as-an-example-ex3",
      "language": "python",
      "code": "from redisvl.query import VectorQuery\n\nvec = emb_model.embed(\"I'd like a bike for aggressive riding\")\n\nv = VectorQuery(\n    vector=vec,\n    vector_field_name=\"bike_embedding\",\n    return_fields=[\n        \"brand\",\n        \"name\",\n        \"$.metadata.type\"\n    ]\n)\n\n\nresults = bike_index.query(v)",
      "section_id": "as-an-example"
    },
    {
      "id": "as-an-example-ex4",
      "language": "python",
      "code": "results",
      "section_id": "as-an-example"
    },
    {
      "id": "next-steps-ex0",
      "language": "python",
      "code": "# Cleanup\nbike_index.delete()",
      "section_id": "next-steps"
    }
  ]
}
