简体   繁体   中英

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

This is just a part of my json file which 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 a Python scipt which download this file.. i want to parse this file and remove a number of objects like "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:
  with open('download.json', 'w') as json_data:
    print m.get_body()
    json_data.write(m.get_body())
    json_data.close()

    # I want a logic here which can simply delete the specific json objects
    # Something like this is what i tried but didn't work...
    #    clean_data = json.load(json_data)   
    #    for element in clean_data:         ##
    #      del element['sshdsakey'] 
    #      json_data.write(clean_data)

I basically need to parse the fetched json file and then remove the specific objects and then just write this new modified stuff in a file.

You can parse your json using loads from native json module. Then delete an element from the dict using del

import json

keys_to_remove = ['sshdsakey', 'selinux']

json_str = '''{
    "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"
}'''

data = json.loads(json_str)
for key in keys_to_remove:
    if key in data:
        del data[key]
print data

json.loads will decode JSON string into Python dictionary (Although format you provided is not a valid JSON format, there have to be curly braces on each side), then you can delete the needed keys with del , encode dictionary back to JSON string with json.dumps and write the resultit

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

You need to first convert the JSON object string into a Python dict , delete the keys from it, and then write to to the output file.

import json

sqs = boto.sqs.connect_to_region("ap-west-1")
q = sqs.get_queue("deathvally")
m = q.read(visibility_timeout=15)
if m is None:
    print "No message!"
else:
    KEYS_TO_REMOVE = "swapsize", "sshdsakey", "etc"

    with open('download.json', 'w') as json_data:
        json_obj = json.loads(m.get_body())
        for key in KEYS_TO_REMOVE:
            try:
                del json_obj[key]
            except KeyError:
                pass
        json_data.write(json.dumps(json_obj, indent=4))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM