我有一个包含很多metadeta的json文件。需要使用python脚本解析此json文件,该脚本将删除一些敏感信息,如SSH密钥等。
JSON文件:
{"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"}}
我正在尝试使用基本代码来删除密钥,如下所示:
import json
j = json.load(open("blob.json"))
obj = json.loads(j)
for element in obj:
del element['sshdsakey','sshrsakey']
json_data.write(obj)
但这会引发错误:
Traceback (most recent call last):
File "popp.py", line 4, in <module>
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
我做错了什么配偶?
答案 0 :(得分:1)
因为您正在尝试加载文件对象(open("blob.json")
将生成文件对象)。
试试这个:
with open("blob.json") as f:
obj = json.loads(f.read())
for element in obj:
del element['sshdsakey','sshrsakey']
json_data.write(obj)
问题在于del element['sshdsakey','sshrsakey']
。在for
上使用dict
会将密钥作为字符串返回,因此您正在执行del 'foobar'
之类的操作。
您可以执行del obj[element]['sshrsakey']
之类的操作,但如果没有obj[element]['sshrsakey']
则会引发关键错误。
我有一个使用正则表达式的解决方案,但我确信有更多的pythonic方式(而且我认为这种方式是最糟糕的方式,但也许可行):
import re
with open("blob.json") as f:
s = f.read()
s = re.sub(', "ssh.*key": ".+?"', '', s)
s = re.sub('{"ssh.*key": ".+?", ', '{', s)
s = re.sub(', "ssh.*key": ".+?"}', '}', s)
obj = json.loads(s)
json_data.write(obj)
答案 1 :(得分:0)
只需obj = json.load(open("blob.json"))