Introduction
Serialization helps save complex data in Redis by turning it into a simple string format that Redis can store and retrieve easily.
Use a serialization method like JSON or MessagePack to convert data before saving: SET key serialized_value To get and deserialize: GET key Deserialize the value back to original data
SET user:1 "{\"name\": \"Alice\", \"age\": 30}"
GET user:1 # returns "{\"name\": \"Alice\", \"age\": 30}"
# Using a Redis client in Python import json user = {"name": "Bob", "age": 25} redis_client.set('user:2', json.dumps(user)) stored = redis_client.get('user:2') user_obj = json.loads(stored)
SET product:1001 "{\"id\": 1001, \"name\": \"Coffee Mug\", \"price\": 12.99}" GET product:1001
import redis
import json
r = redis.Redis()
data = {'name': 'Alice', 'age': 30}
r.set('user:1', json.dumps(data))
retrieved = r.get('user:1')
result = json.loads(retrieved.decode())
What will be the value of result?import redis
import json
r = redis.Redis()
my_list = [1, 2, 3]
r.set('numbers', my_list)
But it raises a TypeError. What is the problem?Pass the test and this pattern moves unknown