2017-07-27 58 views
0

我一直在運行一個Python腳本,它刪除每組數據的ID,以允許它上傳到BigQuery。現在,我不想刪除這些信息,而是想將其插入每組數據中,以便稍後參考。從JSON中刪除ID,然後將其添加到每組數據中。

這就是我一直在接收JSON,並將其上傳到BigQuery中的代碼。

{"AppName": "DataWorks", "foundedPeripheralCount": 1, "version": "1.6.1(8056)", "deviceType": "iPhone 6", "createdAt": "2017-04-05T07:05:30.408Z", "updatedAt": "2017-04-05T07:08:49.569Z", "connectedPeripheralCount": 1, "iOSVersion": "10.2.1"} 

-

firebase = firebase.FirebaseApplication('https://dataworks-356fa.firebaseio.com/') 
result = firebase.get('/PeripheralCount', None) 
id_keys = map(str, result.keys()) #filter out ID names 


with open("firetobq_peripheral2.json", "w") as outfile: 
    for id in id_keys: 
    json.dump(result[id], outfile, indent=None) 
    outfile.write("\n") 

這是JSON的方式,而不與代碼一起任何編輯創建。

{"1972FDEE-E2C0-4E8C-BD00-630315D4AEDE": {"AppName": "DataWorks", "foundedPeripheralCount": 1, "version": "1.6.1(8056)", "deviceType": "iPhone 6", "connectedPeripheralCount": 1, "updatedAt": "2017-04-05T07:08:49.569Z", "Peripherals": {"1CA726ED-32B1-43B4-9071-B58BBACE20A8": "Arduino"}, "createdAt": "2017-04-05T07:05:30.408Z", "iOSVersion": "10.2.1"} 

其中1972FDEE-E2C0-4E8C-BD00-630315D4AEDE是ID。

firebase = firebase.FirebaseApplication('https://dataworks-356fa.firebaseio.com/') 
result = firebase.get('/PeripheralCount', None) 
id_keys = map(str, result.keys()) #filter out ID names 


with open("firetobq_peripheralx.json", "w") as outfile: 
    # for id in id_keys: 
    json.dump(result, outfile, indent=None) 
    outfile.write("\n") 

我希望它看起來像這樣。

{"ID": "1972FDEE-E2C0-4E8C-BD00-630315D4AEDE", "AppName": "DataWorks", "foundedPeripheralCount": 1, "version": "1.6.1(8056)", "deviceType": "iPhone 6", "connectedPeripheralCount": 1, "updatedAt": "2017-04-05T07:08:49.569Z", "Peripherals": {"1CA726ED-32B1-43B4-9071-B58BBACE20A8": "Arduino"}, "createdAt": "2017-04-05T07:05:30.408Z", "iOSVersion": "10.2.1"} 

感謝您的幫助!

回答

0

首先定義一個基dict保存您的ID:

out = { 
    "ID": "1972FDEE-E2C0-4E8C-BD00-630315D4AEDE", 
} 

然後,更新out與原點JSON:

out.update(js_dict.get(out.get('ID'))) 

js_dict是你上面提到的JSON。

相關問題