Change json.dumps dictionary values in python -


so have following dict:

my_dict{'key1': 'value1',         'key2': 'value2',         'key3': json.dumps([            {"**subkey1**": "subvalue1", "**subkey2**": "subvalue2",},            {"**subkey1**": "other_subvalue", "**subkey2**":"other_subvalue2"}])        } 

what need somehow made def have check , each subkey2 change value def itself

and subkey1 check if value same second subkey1 please note talking subkey1 have twice.

i don't want set them manually. mean have dict global, , calling many def, need make these changes , check inside each def

what tried is:

def recurse_keys(my_dict, indent = ''):     print(indent+str(key))     if isinstance(my_dict[key], dict):         recurse_keys(my_dict[key], indent+'   ') recurse_keys(my_dict) 

and printing of params, not sure how proceed

example:

my_dict{'name': 'georgi',         'famili': 'ivanov',         'drinks': json.dumps([            {"breakfast": "milk", "lunch": "beer",},            {"breakfast": "tea",       "lunch":"vodka"}]) def test()     ....check if both breakfast same , if not make them so....(all these, mean dict , function self in same file) 

so need check if values 2 breakfast same (without know them) , if not, make them so.

and check if there lunch empty value or 0 , again if not, make so

if want edit json string, easiest way decode python data types d = json.loads(str), edit it, encode string str = json.dumps(d) (python json).

import json  my_dict = {'name': 'georgi',\         'famili': 'ivanov',\         'drinks': json.dumps([\            {"breakfast": "milk", "lunch": "beer",},\            {"breakfast": "tea", "lunch":"vodka"}])};  ddict = json.loads(my_dict["drinks"]) # json str python data types  seen = {}; # store items seen  # each dictionary object in key3 d in range(0,len(ddict)):     k in ddict[d]:         if k in seen:             # update value 1 seen             ddict[d][k] = seen[k];          if k == "lunch" , (ddict[d] == "" or ddict[d] none):             ddict[d] = alternative_lunch_value;          else:             seen[k] = ddict[d][k];  my_dict["drinks"] = json.dumps(ddict);  print(my_dict); 

the result on machine is:

{'drinks': '[{"breakfast": "milk", "lunch": "beer"}, {"breakfast": "milk", "lunch": "beer"}]', 'famili': 'ivanov', 'name': 'georgi'} 

updating dict values

because wanted update values in my_dict can read other modules, rather read values. if wanted read values, can iterate on list ddict follows:

for value in ddict:     print("sub1:{0} sub2:{1}\n".format(value["**subkey1**"], value["**subkey2**"])); 

however, since want update values in existing list, need iterate on list of indexes. shown below...

range() , len()

range(start,end) gives list values start end. a = range(1,4) assigns [1,2,3,4] a. len(a) return number of items in list, 4 in case. using these principals, can iterate through ddict.

for d in range(1,len(ddict):     ddict[d]["**subkey1**"] = new_value; 

hope helps started. if update question more details on want (i.e. example input , output, perhaps psudo code), able give better answer.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -