Python(json.load) - 如何提取所需信息

时间:2016-03-29 06:04:51

标签: python

我有一些看起来像这样的JSON:

{
"Volumes": [
    {
        "Attachments": [],
        "Tags": [
            {
                "Value": "snapshot",
                "Key": "Name"
            },
            {
                "Value": "00:00",
                "Key": "Start"
            },
            {
                "Value": "00:20",
                "Key": "Finish"
            },
            {
                "Value": "2",
                "Key": "Retention"
            }
        ],
        "VolumeId": "vol-11111111"
    },
    {
        "Attachments": [],
        "Tags": [
            {
                "Value": "snapshot",
                "Key": "Name"
            },
            {
                "Value": "00:00",
                "Key": "Start"
            },
            {
                "Value": "00:20",
                "Key": "Finish"
            },
            {
                "Value": "2",
                "Key": "Retention"
            }
        ],
        "VolumeId": "vol-22222222"
    },
    {
        "Attachments": [],
        "Tags": [
            {
                "Value": "snapshot",
                "Key": "Name"
            },
            {
                "Value": "00:00",
                "Key": "Start"
            },
            {
                "Value": "00:20",
                "Key": "Finish"
            },
            {
                "Value": "2",
                "Key": "Retention"
            }
        ],
        "VolumeId": "vol-33333333"
    }
]
}

如果您熟悉AWS,它是一个JSON的编辑位,包含我需要的一些值,具体来说:

VolumeId和Start / Finish / Retention标签的值

我从bash获取JSON,然后将其放在一个文件中供python阅读。 python代码是:

# Determines the number of snapshots that have been discovered. This will be used to iterate over volumes later
snap_num=$(python -c '
import json,sys
obj=json.load(sys.stdin)
print (len(obj["Volumes"]))
' <tmp)

# Shifts number of volumes integer to the left by 1, so that 1 becomes 0, 2 becomes 1 etc.
snap_num=$(( snap_num - 1 ))

# Iterates over volumes and pulls required properties
for ((i=0;i<=snap_num;i++)); do
        # Exports the current iteration (bash var) to the python child process
        export ITER=$i

a=$(python -c '
import json
import sys
import os
iter=os.environ["ITER"]
iter=int(iter)
obj=json.load(sys.stdin)
print obj["Volumes"][iter]["VolumeId"]
' <tmp)

# Converts all discovered volumes properties into an array
mapfile -t b <<< "$a"

echo "$a"
done

我确信这不是最好的方法,但我能够从每个卷中提取VolumeId:

vol-11111111
vol-22222222
vol-33333333

挑战

我现在无法弄清楚如何从JSON中提取信息的开始/结束/保留位并将它们连接到VolumeIds。理想的输出将是类似

vol-1111111_Start-00:00_Finish-00:20_Retention:2
vol-2222222_Start-00:00_Finish-00:20_Retention:2
vol-3333333_Start-00:00_Finish-00:20_Retention:2

格式不是那么重要,只要它在一行上,我就可以提取每卷所需的信息。

如果我不清楚或您需要任何进一步的信息,请告诉我。我开始从Bash转换到Python(输入:boto)然而迫切需要写这个,我知道我可以在bash中做到这一点,如果我可以让python进行JSON解析。

干杯!

编辑: obj的值

{u&#39; Volumes&#39;:[{u&#39; VolumeId&#39;:u&#39; vol-11111111&#39;,u&#39;附件&#39;:[],u&#39 ;标签&#39;:[{u&#39; Ke y&#39;:你&#39;姓名&#39;,你&#39;价值&#39;:你&#39;快照&#39;},{u&#39; ;关键&#39;:你开始&#39;,你&#39;价值&#39;:你&#39; 00&#39;},{u&#39;关键&#39;:你完成&#39;,你&#39;价值&#39;:你&#39; 20&#39;},{u&#39;关键&#39;:你&#39;保留& #39;,你&#39;价值&#39;:你&#39; 2&#39;}}},{u&#39; VolumeId&#39;:你&#39; vol-22222222&#39;,你&#39;附件&#39;:[],你&#39;标签&#39;:[{u&#39; Key&#39;:u&#39;姓名&#39;,你&#39;价值&#39;:你&#39; ;快照&#39;},{u&#39;关键&#39;:你&#39;开始&#39;,你&#39;价值&#39;:你&#39; 00&#39;},{u&#39; 39;关键&#39;:你完成&#39;,你&#39;价值&#39;:你&#39; 20&#39;},{u&#39;关键&#39;:你&# 39;保留&#39;,你&#39;价值&#39;:你&#39;}}}},{u&#39; VolumeId&#39;:u&#39; vol-333333 33&#39;,你&#39;附件&#39;:[],你&#39;标签&#39;:[{u&#39; Key&#39;:u&#39; Name&#39;,u&#39;价值观&#39;:你的眼睛是&#39;},{u&#39;关键&#39;:你开始&#39;,你&#39;价值&#39;:你&#39; 00: 00&#39;},{u&#39; Key&#39;:你&#39;完成&#39;,你&#39;价值&#39;:你&#39; 20:&#39;},{u&#39; 39;关键&#39;:你&#39;保留&#39;,你&#39;价值&#39;:你&#39; 2&#39;}]}]} {u&#39; Volume&#39;:[{u&#39; VolumeId&#39;:u&#39; vol-11111111&#39;,u&#39;附件&#39;:[],u&#39;标签&# 39;:[{u&#39; Ke y&#39;:你&#39;姓名&#39;,你&#39;价值&#39;:你&#39;快照&#39;},{u&#39; Key&# 39;:你开始&#39;,你&#39;价值&#39;:你&#39; 00&#39;},{u&#39;关键&#39;:你完成&#39;,你&#39;价值&#39;:你&#39; 20&#39;},{u&#39;关键&#39;:你&#39;保留& #39;,你&#39;价值&#39;:你&#39; 2&#39;}}},{u&#39; VolumeId&#39;:你&#39; vol-22222222&#39;,你&#39;附件&#39;:[],你&#39;标签&#39;:[{u&#39; Key&#39;:u&#39;姓名&#39;,你&#39;价值&#39;:你&#39; ;快照&#39;},{u&#39;关键&#39;:你&#39;开始&#39;,你&#39;价值&#39;:你&#39; 00&#39;},{u&#39; 39;关键&#39;:你完成&#39;,你&#39;价值&#39;:你&#39; 20&#39;},{u&#39;关键&#39;:你&# 39;保留&#39;,你&#39;价值&#39;:你&#39;}}}},{u&#39; VolumeId&#39;:u&#39; vol-333333 33&#39;,你&#39;附件&#39;:[],你&#39;标签&#39;:[{u&#39; Key&#39;:u&#39; Name&#39;,u&#39;价值观&#39;:你的眼睛是&#39;},{u&#39;关键&#39;:你开始&#39;,你&#39;价值&#39;:你&#39; 00: 00&#39;},{u&#39; Key&#39;:你&#39;完成&#39;,你&#39;价值&#39;:你&#39; 20:&#39;},{u&#39; 39;关键&#39;:你&#39;保留&#39;,你&#39;价值&#39;:你&#39; 2&#39;}]}]} {u&#39; Volume&#39;:[{u&#39; VolumeId&#39;:u&#39; vol-11111111&#39;,u&#39;附件&#39;:[],u&#39;标签&# 39;:[{u&#39; Ke y&#39;:你&#39;姓名&#39;,你&#39;价值&#39;:你&#39;快照&#39;},{u&#39; Key&# 39;:你开始&#39;,你&#39;价值&#39;:你&#39; 00&#39;},{u&#39;关键&#39;:你完成&#39;,你&#39;价值&#39;:你&#39; 20&#39;},{u&#39;关键&#39;:你&#39;保留& #39;,你&#39;价值&#39;:你&#39; 2&#39;}}},{u&#39; VolumeId&#39;:你&#39; vol-22222222&#39;,你&#39;附件&#39;:[],你&#39;标签&#39;:[{u&#39; Key&#39;:u&#39;姓名&#39;,你&#39;价值&#39;:你&#39; ;快照&#39;},{u&#39;关键&#39;:你&#39;开始&#39;,你&#39;价值&#39;:你&#39; 00&#39;},{u&#39; 39;关键&#39;:你完成&#39;,你&#39;价值&#39;:你&#39; 20&#39;},{u&#39;关键&#39;:你&# 39;保留&#39;,你&#39;价值&#39;:你&#39;}}}},{u&#39; VolumeId&#39;:u&#39; vol-333333 33&#39;,你&#39;附件&#39;:[],你&#39;标签&#39;:[{u&#39; Key&#39;:u&#39; Name&#39;,u&#39;价值观&#39;:你的眼睛是&#39;},{u&#39;关键&#39;:你开始&#39;,你&#39;价值&#39;:你&#39; 00: 00&#39;},{u&#39; Key&#39;:你&#39;完成&#39;,你&#39;价值&#39;:你&#39; 20:&#39;},{u&#39; 39;关键&#39;:你&#39;保留&#39;,你&#39;价值&#39;:你&#39; 2}}}}}}}

Edit2 - 错误消息

Traceback (most recent call last):
File "<string>", line 7, in <module>
NameError: name 'Volumes' is not defined
{u'Volumes': [{u'VolumeId': u'vol-11111111', u'Attachments': [], u'Tags':     [{u'Key': u'Name', u'Value': u'snapshot'}, {u'Key': u'Start', u'Value': u'00:00'}, {u'Key': u'Finish', u'Value': u'00:20'}, {u'Key': u'Retention', u'Value': u'2'}]}, {u'VolumeId': u'vol-22222222', u'Attachments': [], u'Tags': [{u'Key': u'Name', u'Value': u'snapshot'}, {u'Key': u'Start', u'Value': u'00:00'}, {u'Key': u'Finish', u'Value': u'00:20'}, {u'Key': u'Retention', u'Value': u'2'}]}, {u'VolumeId': u'vol-33333333', u'Attachments': [], u'Tags': [{u'Key': u'Name', u'Value': u'snapshot'}, {u'Key': u'Start', u'Value': u'00:00'}, {u'Key': u'Finish', u'Value': u'00:20'}, {u'Key': u'Retention', u'Value': u'2'}]}]}
Traceback (most recent call last):
File "<string>", line 7, in <module>
NameError: name 'Volumes' is not defined
{u'Volumes': [{u'VolumeId': u'vol-11111111', u'Attachments': [], u'Tags': [{u'Key': u'Name', u'Value': u'snapshot'}, {u'Key': u'Start', u'Value': u'00:00'}, {u'Key': u'Finish', u'Value': u'00:20'}, {u'Key': u'Retention', u'Value': u'2'}]}, {u'VolumeId': u'vol-22222222', u'Attachments': [], u'Tags': [{u'Key': u'Name', u'Value': u'snapshot'}, {u'Key': u'Start', u'Value': u'00:00'}, {u'Key': u'Finish', u'Value': u'00:20'}, {u'Key': u'Retention', u'Value': u'2'}]}, {u'VolumeId': u'vol-33333333', u'Attachments': [], u'Tags': [{u'Key': u'Name', u'Value': u'snapshot'}, {u'Key': u'Start', u'Value': u'00:00'}, {u'Key': u'Finish', u'Value': u'00:20'}, {u'Key': u'Retention', u'Value': u'2'}]}]}
Traceback (most recent call last):
  File "<string>", line 7, in <module>
NameError: name 'Volumes' is not defined
{u'Volumes': [{u'VolumeId': u'vol-11111111', u'Attachments': [], u'Tags': [{u'Key': u'Name', u'Value': u'snapshot'}, {u'Key': u'Start', u'Value': u'00:00'}, {u'Key': u'Finish', u'Value': u'00:20'}, {u'Key': u'Retention', u'Value': u'2'}]}, {u'VolumeId': u'vol-22222222', u'Attachments': [], u'Tags': [{u'Key': u'Name', u'Value': u'snapshot'}, {u'Key': u'Start', u'Value': u'00:00'}, {u'Key': u'Finish', u'Value': u'00:20'}, {u'Key': u'Retention', u'Value': u'2'}]}, {u'VolumeId': u'vol-33333333', u'Attachments': [], u'Tags': [{u'Key': u'Name', u'Value': u'snapshot'}, {u'Key': u'Start', u'Value': u'00:00'}, {u'Key': u'Finish', u'Value': u'00:20'}, {u'Key': u'Retention', u'Value': u'2'}]}]}

1 个答案:

答案 0 :(得分:1)

使用以下python代码。

假设您的json存储在变量 a : -

volume = a['Volumes']
vol = []
for i in volume:
    r = []
    r.append(i['VolumeId'])
    for tags in i['Tags']:
        if tags['Key'] in ['Start', 'Finish', 'Retention']:
            r.append(tags['Key'] + '-' + tags['Value'])
    vol.append("_".join(r))

这将提供您想要的所需输出..

['vol-11111111_Start-00:00_Finish-00:20_Retention-2', 'vol-22222222_Start-00:00_Finish-00:20_Retention-2', 'vol-33333333_Start-00:00_Finish-00:20_Retention-2']

您的密码:

a=$(python -c '
import json
import sys
import os
iter=os.environ["ITER"]
iter=int(iter)
obj=json.load(sys.stdin)
print obj["Volumes"][iter]["VolumeId"]
' <tmp)

新代码: -

a=$(python -c '
import json
import sys
import os
obj=json.load(sys.stdin)
print obj
volume = obj['Volumes']
vol = []
for i in volume:
    r = []
    r.append(i['VolumeId'])
    for tags in i['Tags']:
        if tags['Key'] in ['Start', 'Finish', 'Retention']:
            r.append(tags['Key'] + '-' + tags['Value'])
    vol.append("_".join(r))

print vol
' <tmp)