python - Remove specific JSON oobjects from a File and then store this file -


this part of json file looks like:

"network_lo": "127.0.0.0",     "ec2_block_device_mapping_root": "/dev/sda1",     "selinux": "false",     "uptime_seconds": 127412,     "ec2_reservation_id": "r-cd786568",     "sshdsakey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",     "ec2_block_device_mapping_ami": "/dev/sda1",     "memorysize": "3.66 gb",     "swapsize": "0.00 kb",     "netmask": "255.255.255.192",     "uniqueid": "24wq0see",     "kernelmajversion": "3.2", 

i have python scipt download file.. want parse file , remove number of objects "swapsize","sshdsakey"

sqs = boto.sqs.connect_to_region("ap-west-1") q = sqs.get_queue("deathvally") m = q.read(visibility_timeout=15) if m == none:   print "no message!" else:   open('download.json', 'w') json_data:     print m.get_body()     json_data.write(m.get_body())     json_data.close()      # want logic here can delete specific json objects     # tried didn't work...     #    clean_data = json.load(json_data)        #    element in clean_data:         ##     #      del element['sshdsakey']      #      json_data.write(clean_data) 

i need parse fetched json file , remove specific objects , write new modified stuff in file.

json.loads decode json string python dictionary (although format provided not valid json format, there have curly braces on each side), can delete needed keys del , encode dictionary json string json.dumps , write resultit

clean_data = json.loads(json_data.read()) del clean_data[your_key] open(your_file_to_write, 'w') f:   f.write(json.dumps(clean_data))   

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -