python - Iterating over a .json file and changing a attribute value -


i'm trying iterate on .json file small script find attribute named 'model' , change it's value on each object. (it's db-dump django).

import codecs, json  data = ""  codecs.open("dump2.json", 'r', 'utf8') data_file:         data = json.load(data_file)  in data:     key in i:         if key[0] == 'model':             key[1] = "app.model" #not working  codecs.open('dump2.json', 'w', 'utf8') f:     json.dump(data, f) 

what doing wrong here? don't errors. , values not changed.

based on random documentation found on web. assume have such json structure:

[     {          "pk": 1,         "model": "auth.user",         "fields":{             "username": "test1"         }     },{         "pk": 2,         "model": "auth.user",         "fields":{             "username": "test2"         }     } ] 

using json example, when code executes iterates correctly on json keys on line for in data:. on second loop for key in i:, iterating on string ("model" string). so, in case, key[0] 'm', key[1] 'o' , on...

you should try way:

import codecs, json  data = ""  codecs.open("dump2.json", 'r', 'utf8') data_file:         data = json.load(data_file)  in data:     key in i:         if key=="model":             i[key] = "app.model"  codecs.open('dump2.json', 'w', 'utf8') f:     json.dump(data, f) 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -