9951 explained code solutions for 126 technologies


python-redisSave nested dict to Redis


We'll use JSON encoding and decoding to store and read nested dicts:

dict = {'name': 'Donald', 'family': {'wife': 'Melany'}}

r = redis.Redis()
r.set('nd', json.dumps(dict))ctrl + c
dict =

sample nested dict to save to Redis

redis.Redis

connect to Redis server

r.set

sets value by key in Redis

'nd'

name of the Redis key to save our dict to

json.dumps(dict)

converts dict to JSON string


Usage example

import redis, json

dict = {'name': 'Donald', 'family': {'wife': 'Melany'}}

r = redis.Redis()
r.set('d1', json.dumps(dict))

res = json.loads(r.get('d1'))
print(res)
output
{'name': 'Donald', 'family': {'wife': 'Melany'}, 'test': 1}