简体   繁体   English

如何使用python将json响应转换为excel

[英]how to convert json response to excel using python

this reponse I am getting:我得到的这个回复:

    {
      "value": [
        {
          "id": "/providers/Microsoft.Billing/Departments/1234/providers/Microsoft.Billing/billingPeriods/201903/providers/Microsoft.Consumption/usageDetails/usageDetails_Id1",
          "name": "usageDetails_Id1",
          "type": "Microsoft.Consumption/usageDetails",
          "kind": "legacy",
          "tags": {
            "env": "newcrp",
            "dev": "tools"
          },
          "properties": {
            "billingAccountId": "xxxxxxxx",
            "billingAccountName": "Account Name 1",
            "billingPeriodStartDate": "2019-03-01T00:00:00.0000000Z",
            "billingPeriodEndDate": "2019-03-31T00:00:00.0000000Z",
            "billingProfileId": "xxxxxxxx",
            "billingProfileName": "Account Name 1",
            "accountName": "Account Name 1",
            "subscriptionId": "00000000-0000-0000-0000-000000000000",
            "subscriptionName": "Subscription Name 1",
            "date": "2019-03-30T00:00:00.0000000Z",
            "product": "Product Name 1",
            "partNumber": "Part Number 1",
            "meterId": "00000000-0000-0000-0000-000000000000",
            "meterDetails": null,
            "quantity": 0.7329,
            "effectivePrice": 0.000402776395232,
            "cost": 0.000295194820065,
            "unitPrice": 4.38,
            "billingCurrency": "CAD",
            "resourceLocation": "USEast",
            "consumedService": "Microsoft.Storage",
            "resourceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/Resource Group 1/providers/Microsoft.Storage/storageAccounts/Resource Name 1",
            "resourceName": "Resource Name 1",
            "invoiceSection": "Invoice Section 1",
            "costCenter": "DEV",
            "resourceGroup": "Resource Group 1",
            "offerId": "Offer Id 1",
            "isAzureCreditEligible": false,
            "chargeType": "Usage",
            "benefitId": "00000000-0000-0000-0000-000000000000",
            "benefitName": "Reservation_purchase_03-09-2018_10-59"
          }
        },
{
          "id": "/providers/Microsoft.Billing/Departments/1234/providers/Microsoft.Billing/billingPeriods/201903/providers/Microsoft.Consumption/usageDetails/usageDetails_Id1",
          "name": "usageDetails_Id1",
          "type": "Microsoft.Consumption/usageDetails",
          "kind": "legacy",
          "tags": {
            "env": "newcrp",
            "dev": "tools"
          },
          "properties": {
            "billingAccountId": "xxxxxxxx",
            "billingAccountName": "Account Name 1",
            "billingPeriodStartDate": "2019-03-01T00:00:00.0000000Z",
            "billingPeriodEndDate": "2019-03-31T00:00:00.0000000Z",
            "billingProfileId": "xxxxxxxx",
            "billingProfileName": "Account Name 1",
            "accountName": "Account Name 1",
            "subscriptionId": "00000000-0000-0000-0000-000000000000",
            "subscriptionName": "Subscription Name 1",
            "date": "2019-03-30T00:00:00.0000000Z",
            "product": "Product Name 1",
            "partNumber": "Part Number 1",
            "meterId": "00000000-0000-0000-0000-000000000000",
            "meterDetails": null,
            "quantity": 0.7329,
            "effectivePrice": 0.000402776395232,
            "cost": 0.000295194820065,
            "unitPrice": 4.38,
            "billingCurrency": "CAD",
            "resourceLocation": "USEast",
            "consumedService": "Microsoft.Storage",
            "resourceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/Resource Group 1/providers/Microsoft.Storage/storageAccounts/Resource Name 1",
            "resourceName": "Resource Name 1",
            "invoiceSection": "Invoice Section 1",
            "costCenter": "DEV",
            "resourceGroup": "Resource Group 1",
            "offerId": "Offer Id 1",
            "isAzureCreditEligible": false,
            "chargeType": "Usage",
            "benefitId": "00000000-0000-0000-0000-000000000000",
            "benefitName": "Reservation_purchase_03-09-2018_10-59"
          }
        }
      ]
    }

code:代码:

import pandas as pd
frame=pd.DataFrame()
for i in range (len(json_output['value'])):
    
    df1= pd.DataFrame(data={'kind':json_output['value'][i]['kind'],
    'id': json_output['value'][i]['id'],
    'tags': json_output['value'][i]['tags'],
    'name':json_output['value'][i]['name'],
    'type':json_output['value'][i]['type'],
    'billingAccountid':json_output['value'][i]['properties']['billingAccountId']},index=[i])
    print(df1)                
    frame=frame.append(df1)
    
frame.to_csv('datt.csv')

Can you please help me to convert this data in to csv.你能帮我把这些数据转换成csv吗? I am looking for id,name,type,kind,tags,billingAccountId,resourceName etc into all column我正在寻找 id、name、type、kind、tags、billingAccountId、resourceName 等到所有列

I tried to convert into DataFrame it didn't work.我试图转换成 DataFrame 它不起作用。 At last I am trying above python but its giving tags into null.最后,我在 python 之上尝试,但它的标签为空。 Note : I want to keep tags in dict format (for now)注意:我想保持 dict 格式的标签(现在)

I tried your code and stored json file into an output first:我尝试了您的代码并将json文件存储到输出中:

输出

-TAGS is a dictionary you access it without any keys so it will be NONE -TAGS 是一个字典,您无需任何键即可访问它,因此它将为NONE

If not comfortable by splitting TAGS use:如果通过拆分TAGS感到不舒服,请使用:

'tags':json_output['value'][i]['tags']['env']+json_output['value'][i]['tags']['dev']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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