2016-01-15 174 views
-2

我從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() 

但沒有成功的工作,因爲男,任何嵌套版本,也在一些響應中,我們有一些標題需要檢查,如果它不在那裏,然後爲此創建一個新標題

再次感謝所有幫助! 馬努

+0

定義什麼結果應該看起來像第一位的。 – deceze

回答

1

首先,安裝熊貓:

pip install pandas 

然後,用熊貓來使用你從響應獲取的數據創建一個數據幀的對象。創建對象時,您可以將其轉換爲csv或xls文件,並設置'index = False'以防止將索引添加到輸出文件中。

import pandas as pd 
import json 

with open('data_new.json') as fi: 
    data = json.load(fi) 
    df = pd.DataFrame(data=data['rows'],columns=data['headings']) 
    df.to_csv('data_table.csv', index=False) 

輸出例如:

ACCOUNT_ID,date,FB Likes 
My Account,1435708800000,117 
My Account,1435795200000,99 
My Account,1435708800000,7 
+0

僅由代碼組成的解答,沒有解釋,被認爲沒那麼有用。請考慮添加一些說明文字。 –

+0

您可能需要先安裝熊貓。做到這一點:'$ pip install pandas' –

+0

嗨羅馬 - 它只是像魔術一樣工作,我無話可說要感謝你,我一直在尋找這個無處不在!再次感謝。但是,這是我的問題開始的地方,我有一些更深的嵌套API Json響應,甚至不適用於此代碼。如果可能的話,我會請求看看上面編輯過的我的問題,我也包含了其他Json響應。你是一個救世主 –

0

錯過了蟒蛇的要求,但如果你願意來調用外部程序,這將仍然正常工作。 請注意,這需要jq> = 1.5。

cat YourJsonFile | jq -r ' [ .rows[][0] | to_entries | map(.key), map(.value | tostring) ] | .[0,range(1;length;2)]|@csv' 

# Lets break it down 
jq -r # disable escaping and quoting 
    ' [ # this will add create an array 
     .rows[][0] # select rows (from object, [] it's array, 
       # and [0] first element in that array) 
     | to_entries # convert it to key, value object 
     | map(.key), map(.value | tostring) # select key and value 
          # (value is converted to string) 
          # this is the step that needs '-r' option to jq 
     ] # close array. We now have alternating "header" and "data" rows 
     | .[0,range(1;length;2)] # select from current (.), first (0) and 
           # with range function every second row 
           # starting from one 
     |@csv # convert resulting json to csv 
     '  # Done 

https://stedolan.github.io/jq/