0
0
Redisquery~10 mins

Serialization considerations in Redis - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to serialize a Python dictionary to a JSON string before storing it in Redis.

Redis
import json
import redis
r = redis.Redis()
data = {'name': 'Alice', 'age': 30}
r.set('user:1', [1])
Drag options to blanks, or click blank then click option'
Astr(data)
Bjson.loads(data)
Cjson.dumps(data)
Ddata.encode()
Attempts:
3 left
💡 Hint
Common Mistakes
Using json.loads which deserializes JSON instead of serializing.
Trying to encode the dictionary directly without converting to string.
2fill in blank
medium

Complete the code to deserialize a JSON string retrieved from Redis back into a Python dictionary.

Redis
import json
import redis
r = redis.Redis()
json_data = r.get('user:1')
data = [1]
Drag options to blanks, or click blank then click option'
Ajson.loads(json_data.decode('utf-8'))
Bjson.loads(json_data)
Cjson.dumps(json_data)
Djson.dumps(json_data.decode('utf-8'))
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to load JSON directly from bytes without decoding.
Using json.dumps instead of json.loads for deserialization.
3fill in blank
hard

Fix the error in the code that tries to store a Python set in Redis by serializing it.

Redis
import json
import redis
r = redis.Redis()
my_set = {1, 2, 3}
r.set('myset', [1])
Drag options to blanks, or click blank then click option'
Astr(my_set)
Bjson.dumps(list(my_set))
Cjson.dumps(my_set)
Dmy_set.encode()
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to serialize the set directly causing an error.
Using str() which is not JSON format.
4fill in blank
hard

Fill both blanks to correctly serialize a nested Python object with a datetime before storing it in Redis.

Redis
import json
import redis
from datetime import datetime
r = redis.Redis()
data = {'event': 'login', 'time': datetime.now()}

class DateTimeEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.[1]()
        return super().default(obj)
r.set('event', json.dumps(data, cls=[2]))
Drag options to blanks, or click blank then click option'
Aisoformat
Btimestamp
CDateTimeEncoder
DDateEncoder
Attempts:
3 left
💡 Hint
Common Mistakes
Using timestamp instead of isoformat which is more readable.
Passing wrong class name to json.dumps.
5fill in blank
hard

Fill all three blanks to deserialize JSON from Redis and convert the datetime string back to a datetime object.

Redis
import json
import redis
from datetime import datetime
r = redis.Redis()
json_str = r.get('event').decode('utf-8')

def datetime_parser(dct):
    for k, v in dct.items():
        if k == '[1]':
            dct[k] = datetime.[2](v)
    return dct

data = json.loads(json_str, object_hook=[3])
Drag options to blanks, or click blank then click option'
Atime
Bfromisoformat
Cdatetime_parser
Disoformat
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong key name in the parser function.
Not passing the parser function to json.loads.
Using isoformat instead of fromisoformat for parsing.