如何用Python将JSON日期以毫秒为单位转换为Cloud Function中的可读格式



我有一个云函数,该函数通过选择查询连接到Salesforce Bulk API,该查询返回JSON格式的数据,然后上传到Google云存储桶。CF本身工作没有问题,但是其中包含日期的字段返回以毫秒为单位的日期。

我正在寻找一种将此日期从毫秒转换为可读日期时间格式的方法。

def test_query():
job = bulk.create_query_job("Person", contentType='JSON')
batch = bulk.query(job, "SELECT Name, Id, Date FROM Person")
bulk.close_job(job)
while not bulk.is_batch_done(batch):
sleep(10)

for result in bulk.get_all_results_for_query_batch(batch):
result = json.load(IteratorBytesIO(result))
for row in result:
print(row)# dictionary rows
# Push data to GCS
blob = bucket.blob('PersonObj.json')
json_string = json.dumps(result)
blob.upload_from_string(json_string)

一行JSON 的结果

"Name": "John Smith",
"Id": "0011E01231fAn1BWTF",
"Date": 1550602301000

希望拥有";日期";以可读的形式返回,例如yyyy-mm-dd hh:mm:ss.str

也许这是可以使用CSV方法或在BigQuery中完成的事情,因为这是我最终打算使用这些数据的地方。

谢谢!

jsonBlob = {"Name": "John Smith",
"Id": "0011E01231fAn1BWTF",
"Date": 1550602301000}
datetime.fromtimestamp(jsonBlob["Date"]/1000.0)

由于没有实际数据,不确定2019-02-19 12:51:41是否为实际日期。也许用以上解决