从json列表数组中提取元素

jvidinwx  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(179)
[
  {     
    "confirmed": false,
    "client": {       
      "Outcome": {
        "ResponseId": "64352036ead00dc1fd8d",
        "Options": [
          {
            "optionState": "AUTHORISED",
           
            "Endorsements": [
              {
                "PointId": "7e8fb6e0-0435-45c3-b2a5-82cb2074bf63", 
                "LifetimeIds": ["00002"], 
                "Code": "001",
                "Name": "Name of the provider",
                "Wording": "Wording goes here",
                "Variables": [{ "name": "${value}", "value": "450" }]
              }
            ],
            "subscriptions": [             
              {                
                "totalAmount": { "amount": "450.0", "currencyCode": "EUR" },
                "tax": { "amount": "0.0", "currencyCode": "EUR" },
                "subscription": { "amount": "450.0", "currencyCode": "EUR" },
                "code": "Movie",
                "message": "You have access to all movies in the library"
              },
              {               
                "totalAmount": { "amount": "25.0", "currencyCode": "EUR" },
                "tax": { "amount": "0.0", "currencyCode": "EUR" },
                "subscription": { "amount": "25.0", "currencyCode": "EUR" },
                "code": "Library",
                "message": "You are able to borrow books"
              }
            ]           
          }
        ]
       }       
      },
     
    "startDate": "17-04-2023",   
    "name": "Joe Blogg",
    "email": "joe@blogg.com",
    "phoneNumber": "01234567890" 
  }
]

我想知道是否有人可以帮助我,我试图循环通过'subscribes'元素并只打印出['tax']['amout']和['totalAmount']['amount'] -将它们添加到字典中。

list indices must be integers or slices, not str

所以我希望有一个新的字典,有这样的格式:

"subscriptions": [             
              {                
                "totalAmount": { "amount": "450.0" },
                "tax": { "amount": "0.0"}
                
              },
              {               
                "totalAmount": { "amount": "25.0"},
                "tax": { "amount": "0.0"}
                                
              }
            ]
new_dict = {}
for i in data['client']['Outcome']['Options']['subscriptions']:
    new_dict.update({'totalAmount': i['totalAmount']['amount']})
    new_dict.update({'tax': i['tax']['amount']})

代码是我试图用来解析它的,但是它给了我错误-所以我不能用我感兴趣的值构建一个新的字典。
任何帮助将不胜感激。

nwnhqdif

nwnhqdif1#

如果每个列表只有一条记录,则可以用途:

new_dict = {'subscriptions': []}

#                 HERE --v                           HERE --v
for subscription in data[0]['client']['Outcome']['Options'][0]['subscriptions']:
    new_dict['subscriptions'].append({'totalAmount': {'amount': subscription['totalAmount']['amount']},
                                      'tax': {'amount': subscription['tax']['amount']}})

输出:

>>> json.dumps(new_dict, indent=4)
{
    "subscriptions": [
        {
            "totalAmount": {
                "amount": "450.0"
            },
            "tax": {
                "amount": "0.0"
            }
        },
        {
            "totalAmount": {
                "amount": "25.0"
            },
            "tax": {
                "amount": "0.0"
            }
        }
    ]
}

相关问题