使用标头将JSON响应从API转换为CSV文件

时间:2019-02-01 13:56:30

标签: python-3.x

我正在尝试从Microsoft Azure检索用户列表并将结果放入csv文件中

我能够以json格式检索用户列表

users_list = requests.get(url, headers={'Content-Type':'application/json','Authorization': 'Bearer ' +access_token})
print(users_list.json())
json_data = users_list.json()

json_data以以下格式保存信息

这是json的示例:

  

{“ @ odata.context”:“ https://graph.microsoft.com/v1.0/ $ metadata#users”,“ @ odata.nextLink”:“ https://graph.microsoft.com/v1.0/users?$ skiptoken = X%27445370740200010001000F3A746573763763376432653236632D373438612D3432600000000000000%27”,“值” :[{“ businessPhones”:[],“ displayName”:“ Br   Krty“,” givenName“:” Brrty“,” jobTitle“:null,” mail“:” bkrty@test.microsoft.com“,” mobilePhone“:null,” officeLocation“:null,” preferredLanguage“:null,”姓氏:“ Kwan”,“ userPrincipalName”:“ btert@test.microsoft.com”,“ id”:“ 6drty631c”},{“ businessPhones”:[],“ displayName”:“ rty   Srtyti“,” givenName“:” Crty“,” jobTitle“:null,” mail“:” crty@test.microsoft.com“,” mobilePhone“:null,” officeLocation“:null,” preferredLanguage“:null,”姓氏::“ Surty”,“ userPrincipalName”:“ rtyty@test.microsoft.com”,“ id”:“ y122rtye604d”},{“ businessPhones”:[],“ displayName”:“ rty   Crty“,” givenName“:” Drty“,” jobTitle“:null,” mail“:” rtyr@test.microsoft.com“,” mobilePhone“:null,” officeLocation“:null,” preferredLanguage“:null,”姓“:” Crtytyr“,” userPrincipalName“:” drtyr@test.microsoft.com“,” id“:” rty8drty24“}]}

并采用更具可读性的格式:

  

{
  “ @ odata.context”:“ https://graph.microsoft.com/v1.0/ $ metadata#users”,
  “ @ odata.nextLink”:“ https://graph.microsoft.com/v1.0/users?$ skiptoken = X%27445370740200010000001F3A746573763376432653236632D373438612D3432600000000000000%27”,   “值”:[
        {
           “ businessPhones”:[

     ],
     "displayName":"Br Krty",
     "givenName":"Brrty",
     "jobTitle":null,
     "mail":"bkrty@test.microsoft.com",
     "mobilePhone":null,
     "officeLocation":null,
     "preferredLanguage":null,
     "surname":"Kwan",
     "userPrincipalName":"btert@test.microsoft.com",
     "id":"6drty631c"
  },
  {  
     "businessPhones":[  

     ],
     "displayName":"rty Srtyti",
     "givenName":"Crty",
     "jobTitle":null,
     "mail":"crty@test.microsoft.com",
     "mobilePhone":null,
     "officeLocation":null,
     "preferredLanguage":null,
     "surname":"Surty",
     "userPrincipalName":"rtyty@test.microsoft.com",
     "id":"y122rtye604d"
  },
  {  
     "businessPhones":[  

     ],
     "displayName":"rty Crty",
     "givenName":"Drty",
     "jobTitle":null,
     "mail":"rtyr@test.microsoft.com",
     "mobilePhone":null,
     "officeLocation":null,
     "preferredLanguage":null,
     "surname":"Crtytyr",
     "userPrincipalName":"drtyr@test.microsoft.com",
     "id":"rty8drty24"
  }    ] }

预期的csv应该如下所示:

displayName,mail,userPrincipalName,id

value1,value2,value3,value4

0 个答案:

没有答案