{'action_name':'mobile signup',
'functions':[{'name':'test_signUp',
'parameters':{'username':'[email protected]',
'password':'12345',
'mobileLater':'123454231',
'mobile':'1e2w1e2w',
'card':'1232313',
'cardLater':'1234321234321'}}],
'validations':[
{'MOB_header':'My stores'},
{'url':"/stores/my"}]}
I want to get all the keys & values of this dict as a list (out of values that they are dict or array)
print result should be like this:
action name = mobile signup
name = test_signUp
username : [email protected]
password : 12345
mobileLater: 123454231
mobile : 1e2w1e2w
card : 1232313
cardLater : 1234321234321
MOB_header : My stores
The Redis KEYS command returns all the keys in the database that match a pattern (or all the keys in the key space). Similar commands for fetching all the fields stored in a hash is HGETALL and for all fetching the members of a SMEMBERS. The keys in Redis themselves are stored in a dictionary (aka a hash table).
To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.
What is Redis? Redis, which stands for Remote Dictionary Server, is a fast, open source, in-memory, key-value data store. The project started when Salvatore Sanfilippo, the original developer of Redis, wanted to improve the scalability of his Italian startup.
Basic Operations As a dictionary, Redis allows you to set and retrieve pairs of keys and values. Think of a “key” as a unique identifier (string, integer, etc.) and a “value” as whatever data you want to associate with that key. Values can be strings, integers, floats, booleans, binary, lists, arrays, dates, and more.
I have modified a little bit from this link to get all keys&values in nested dict of list-of-dicts and dicts:
def recursive_items(dictionary):
for key, value in dictionary.items():
if type(value) is dict:
yield (key, value)
yield from recursive_items(value)
elif type(value) is list:
yield (key, value)
for i in value:
if type(i) is dict:
yield from recursive_items(i)
else:
yield (key, value)
for i in recursive_items(your_dict):
print(i) #print out tuple of (key, value)
Output:
('action_name', 'mobile signup')
('functions', [{'name': 'test_signUp', 'parameters': {'username':
'[email protected]', 'password': '12345', 'mobileLater': '123454231', 'mobile':
'1e2w1e2w', 'card': '1232313', 'cardLater': '1234321234321'}}])
('name', 'test_signUp')
('parameters', {'username': '[email protected]', 'password': '12345',
'mobileLater': '123454231', 'mobile': '1e2w1e2w', 'card': '1232313',
'cardLater': '1234321234321'})
('username', '[email protected]')
('password', '12345')
('mobileLater', '123454231')
('mobile', '1e2w1e2w')
('card', '1232313')
('cardLater', '1234321234321')
('validations', [{'MOB_header': 'My stores'}, {'url': '/stores/my'}])
('MOB_header', 'My stores')
('url', '/stores/my')
You might want to use a recursive function to extract all the key, value
pairs.
def extract(dict_in, dict_out):
for key, value in dict_in.iteritems():
if isinstance(value, dict): # If value itself is dictionary
extract(value, dict_out)
elif isinstance(value, unicode):
# Write to dict_out
dict_out[key] = value
return dict_out
Something of this sort. I come from C++ background so I had to google for all the syntaxes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With