title: Read a JSON file using Python & remove certain elements if they exist and writeback to same/new file
body: <p>I am having a json file which contains a lot of metadeta. This json file needs to be parsed using a python script which will remove some sensitive information like SSH Keys etc.</p>
  
  <p>JSON file :</p>
  
  <pre><code>{"instanceid": "sfaf", "webapps": {"jolokia": {"Created-By": "Apache Maven", "Build-Jdk": "1.7.0_11", "Manifest-Version": "1.0", "Built-By": "roland", "Archiver-Version": "Plexus Archiver"}, "SC": {"Name": "cver/", "Manifest-Version": "1.0", "Ant-Version": "Apache Ant 1.8.3", "Specification-Vendor": "Sc.", "Implementation-Title": "dgdgd", "Implementation-Version": "3.4.85", "Sealed": "false", "Specification-Version": "1.1", "Specification-Title": "SaaS License Server", "Build-Date": "2013-12-19 09", "Implementation-Vendor": "Saet Inc.", "Created-By": "1.6.0_43-b01 (Sun Microsystems Inc.)"}}, "facter": {"kernelrelease": "3.2.0-65-virtual", "selinux": "false", "memorytotal": "1.65 GB", "swapfree": "896.00 MB", "ec2_block_device_mapping_swap": "sda3", "ec2_public_ipv4": "5.2.1.0", "ec2_placement_availability_zone": "us-west-1a", "operatingsystem": "Ubuntu", "lsbmajdistrelease": "12", "rubyversion": "1.8.7", "ipaddress_lo": "127.0.0.1", "facterversion": "1.6.5", "is_virtual": "false", "network_lo": "127.0.0.0", "ec2_block_device_mapping_root": "/dev/sda1", "memoryfree": "1.20 GB", "uptime_seconds": 12823192, "ec2_reservation_id": "r-515b720f", "ec2_local_ipv4": "10.188.22.97", "ec2_block_device_mapping_ami": "/dev/sda1", "memorysize": "1.65 GB", "swapsize": "896.00 MB", "ec2_public_keys_0_openssh_key": "ssh-rsa NFb1BSbJkNEHpW35/anJMqw/s6x+ykYELuOPk2JLt andy", "uniqueid": "bc0a6116", "processorcount": "1", "kernelmajversion": "3.2", "macaddress": "22:00:0a:bc:16:61", "ec2_hostname": "ip-.us-west-1.compute.internal", "network_eth0": "101", "uptime_hours": 3561, "ec2_security_groups": "na-prod-1w-secgroup", "rubysitedir": "/usr/local/lib/site_ruby/1.8", "sshecdsakey": "fsafsfsafsasa=", "architecture": "amd64", "netmask_eth0": "255.255.255.192", "arp": "fe:ff:ff:ff:ff:ff", "netmask_lo": "255.0.0.0", "domain": "us-west-1.compute.internal", "puppetversion": "2.7.11", "kernel": "Linux", "uptime_days": 148, "ec2_ami_launch_index": "0", "ec2_public_hostname": "ec2-50-18-7q-a.us-east-1.compute.amazonaws.com", "augeasversion": "0.10.0", "ec2_instance_type": "m1.small", "ec2_profile": "default-paravirtual", "timezone": "UTC", "hardwareisa": "x86_64", "id": "root", "ec2_ami_id": "ami-a5616be0", "ec2_local_hostname": "ip-10-138-22-27.us-west-1.compute.internal", "uptime": "148 days", "macaddress_eth0": "22:00:0aa:bc:a16:61", "hostname": "i", "lsbdistid": "Ubuntu", "virtual": "physical", "ec2_ami_manifest_path": "(unknown)", "ec2_instance_id": "i-3599826b", "sshdsakey": "WF8D0q7m/TpbMKoUAofpUYwmMmLKyC71yXhh3Q0ZCBT8AAACAbnaEqRnJD1YFrzOHs0H/pVoh/6mEXXKeoL9MYhZDGUNhNKhIKvIABCX9tK1jJEItZwnGxRvyEI=", "arp_eth0": "fe:ff:ff:ff:ff:ff", "hardwaremodel": "x86_64", "osfamily": "Debian", "sshrsakey": "AAAAB3NzaC1yc2EAAAADAsTafQ/QHvoFMycI0oo3qG5zCPI5c4NmlQDmTXP4xArXYy3oltG4mYly6MwqxELFcWKpaM1f0TCFFzCAVe8XUyyp7qt7qAZ4aJ+LbFd+TLJrE+H", "ps": "ps -ef", "interfaces": "eth0,lo", "physicalprocessorcount": 1, "ec2_kernel_id": "aki-880531cd", "path": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "ipaddress": "10.188.22.97", "lsbdistdescription": "Ubuntu 12.04.4 LTS", "kernelversion": "3.2.0", "operatingsystemrelease": "12.04", "processor0": "Intel(R) Xeon(R) CPU E5-2651 v2 @ 1.80GHz", "fqdn": "ip-10-188-22-97.us-west-1.compute.internal", "lsbdistcodename": "precise", "lsbdistrelease": "12.04", "ipaddress_eth0": "10.188.22.97", "netmask": "255.255.255.192"}, "os_packages": {"tomcat7": "7.0.26-1ubuntu1.2", "openjdk-6-jre-headless": "6b31-1.13.3-1ubuntu1~0.12.04.2", "linux-image-virtual": "3.2.0.65.77", "openssl": "1.0.1-4ubuntu5.16", "openssh-server": "1:5.9p1-5ubuntu1.4"}}
  </code></pre>
  
  <p>I am trying out a basic code just to remove the keys as follows:</p>
  
  <pre><code>import json
  
  j = json.load(open("blob.json"))
  obj = json.loads(j)
  for element in obj:
      del element['sshdsakey','sshrsakey'] 
  
  json_data.write(obj)
  </code></pre>
  
  <p>But this throws a error: </p>
  
  <pre><code>Traceback (most recent call last):
    File "popp.py", line 4, in &lt;module&gt;
      obj = json.loads(j)
    File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
      return _default_decoder.decode(s)
    File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
      obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  TypeError: expected string or buffer
  </code></pre>
  
  <p>What am i doing wrong mates?</p>