我從REST的API接收幾個JSON迴應,我的迴應的格式如下: -嵌套JSON到CSV轉換
{
"headings": [
"ACCOUNT_ID",
"date",
"FB Likes"
],
"rows": [
[
"My Account",
"1435708800000",
117
],
[
"My Account",
"1435795200000",
99
],
[
"My Account",
"1435708800000",
7
]
]
}
凡列帳戶ID,日期和FB_Likes,我試圖把它轉換成csv,我嘗試了許多不同的迭代,但沒有成功。
請幫我這個
我的一個使用的腳本都是
with open('Account_Insights_12Jan.json') as fi:
data = json.load(fi)
json_array=data
columns = set()
for item in json_array:
columns.update(set(item))
# writing the data on csv
with open('Test_14Jan.csv', 'w', newline='') as fo:
writer = csv.writer(fo)
writer.writerow(list(columns))
for item in json_array:
row = []
for c in columns:
if c in item: row.append(str(item[c]))
else: row.append('')
writer.writerow(row)
NI是從它接收錯誤,我複製它從什麼地方,請解釋如何將它轉換
Hi Again
{
"headings": [
"POST_ ID",
"POST_COMMENT_COUNT"
],
"rows": [
[
{
"postId": 188365573,
"messageId": 198365562,
"accountId": 214,
"messageType": 2,
"channelType": "TWITTER",
"accountType": "TWITTER",
"taxonomy": {
"campaignId": "2521_4",
"clientCustomProperties": {
"PromotionChannelAbbreviation": [
"3tw"
],
"PromotionChannels": [
"Twitter"
],
"ContentOwner": [
"Audit"
],
"Location": [
"us"
],
"Sub_Category": [
"dbriefs"
],
"ContentOwnerAbbreviation": [
"aud"
],
"PrimaryPurpose_Outcome": [
"Engagement"
],
"PrimaryPurposeOutcomeAbbv": [
"eng"
]
},
"partnerCustomProperties": {},
"tags": [],
"urlShortnerDomain": "2721_spr.ly"
},
"approval": {
"approvalOption": "NONE",
"comment": ""
},
"status": "SENT",
"createdDate": 1433331585000,
"scheduleDate": 1435783440000,
"version": 4,
"deleted": false,
"publishedDate": 1435783441000,
"statusID": "6163465412728176",
"permalink": "https://twitter.com/Acctg/status/916346541272498176",
"additional": {
"links": []
}
},
0
],
[
{
"postId": 999145171,
"messageId": 109145169,
"accountId": 21388,
"messageType": 2,
"channelType": "TWITTER",
"accountType": "TWITTER",
"taxonomy": {
"campaignId": "2521_4",
"clientCustomProperties": {
"PromotionChannelAbbreviation": [
"3tw"
],
"Eminence_Registry_Number": [
"1000159"
],
"PromotionChannels": [
"Twitter"
],
"ContentOwner": [
"Ctr. Health Solutions"
],
"Location": [
"us"
],
"Sub_Category": [
"fraud"
],
"ContentOwnerAbbreviation": [
"chs"
],
"PrimaryPurpose_Outcome": [
"Awareness"
],
"PrimaryPurposeOutcomeAbbv": [
"awa"
]
},
"partnerCustomProperties": {},
"tags": [],
"urlShortnerDomain": "2521_spr.ly"
},
"approval": {
"approvalOption": "NONE",
"comment": ""
},
"status": "SENT",
"createdDate": 1434983660000,
"scheduleDate": 1435753800000,
"version": 4,
"deleted": false,
"publishedDate": 1435753801000,
"statusID": "616222222198407168",
"permalink": "https://twitter.com/Health/status/6162222221984070968",
"additional": {
"links": []
}
},
0
]
}
請考慮這個JSON響應 再次感謝fr所有的幫助,你是救世主!
響應將如下所示。這是一個樣本輸出,因爲有很多列,我只包括其中的幾個。我的壞,我不知道如何共享一個Excel輸出
帖子ID,郵件ID,帳戶ID,爲messageType,ACCOUNTTYPE,通道類型
188365573,198365562,214,2,微博,微博
999145171 ,109145169,21388,2,微博,微博
過程中的代碼是
csvdata= open('Data_table2.csv', 'w')
csvwriter = csv.writer(csvdata, delimiter=',')
csvwriter.writerow(header)
for i in range(0,70):
csvwriter.writerow(data1["rows"][i][0].values())
csvdata.close()
但沒有成功的工作,因爲男,任何嵌套版本,也在一些響應中,我們有一些標題需要檢查,如果它不在那裏,然後爲此創建一個新標題
再次感謝所有幫助! 馬努
定義什麼結果應該看起來像第一位的。 – deceze