Reputation: 171
I need some help getting the message received by a Redis stream in python. For example if I send the following data through the stream:
{
"identifier": "abcxyz"
"query": "testQuery"
}
I receive the following from the stream:
[['streamA', [('1611593593691-0', {'identifier': 'zxkbdnarrz', 'query': 'testQuery'})]]]
Here is how I connect to the Redis Stream:
r1 = redis.Redis(host=redis_host, port=redis_port,
password=redis_password, db=1, decode_responses=True)
fromStreamA = r1.xread({'streamA': "$"}, count=1, block=0)
Ideally I would like to be able to access everything between the {}
so that I can use the Python JSON module to access it as a dict.
How can I do this? TIA!
Upvotes: 2
Views: 3552
Reputation: 633
Hi you can do something like this:
msg = [['streamA', [('1611593593691-0', {'identifier': 'zxkbdnarrz', 'query': 'testQuery'})]]]
[[stream, [[number, d]]]] = msg
print(d)
>>> {'identifier': 'zxkbdnarrz', 'query': 'testQuery'}
or when your Message look like this:
msg = [[b'streamA', [[b'1627026892700-0', [b'identifier', b'zxkbdnarrz', b'query', b'testQuery']]]]]
you can get the dict with this code:
msg = [[b'streamA', [[b'1627026892700-0', [b'identifier', b'zxkbdnarrz', b'query', b'testQuery']]]]]
[[stream, [[number, parts]]]] = msg
foo = (p.decode('utf-8') for p in parts)
d = dict(zip(foo, foo))
print(d)
>>> {'identifier': 'zxkbdnarrz', 'query': 'testQuery'}
Upvotes: 2