英文:
Add timestamp to API call JSON output
问题
{"date_time": "2023-06-26 12:41:49", "account": "act_F-AC-1234", "contract": "ctr_F-1234"}
{"date_time": "2023-06-26 12:42:49", "account": "act_F-AC-5678", "contract": "ctr_F-5678"}
英文:
I need to add timestamp in key-value format on a JSON response. This is what I have right now:
now = datetime.datetime.now().isoformat(sep=" ", timespec="seconds")
result = session.get(urljoin(baseurl, path), headers=headers, params=querystring, verify=False)
jsonarray = result.json()
json_object = json.dumps(jsonarray)
#writing to outputfile
with open("/opt/bamboo/logs/outputfile", "a") as outfile:
outfile.write(now + json_object + "\n")
current output looks like this
2023-06-26 12:41:49{"account": "act_F-AC-1234", "contract": "ctr_F-1234"}
2023-06-26 12:42:49{"account": "act_F-AC-5678", "contract": "ctr_F-5678"}
my desired output should be
{"date_time": "2023-06-26 12:41:49", "account": "act_F-AC-1234", "contract": "ctr_F-1234"}
{"date_time": "2023-06-26 12:42:49", "account": "act_F-AC-5678", "contract": "ctr_F-5678"}
please note that the actual API call result (result.json) looks like below in proper format
{
"account": "act_F-AC-1234",
"contract": "ctr_F-1234",
"rules": {
"name": "default",
"children": [
{
"name": "Route to origin",
"children": [],
"behaviors": [
{
"name": "origin",
"options": {
"originType": "CUSTOMER",
"hostname": "elb.test.gateway.origin.com",
thanks in advance
答案1
得分: 1
你将其称为"jsonarray",但它既不是JSON,也不是一个数组。它是一个简单的Python字典。
now = datetime.datetime.now().isoformat(sep=" ", timespec="seconds")
result = session.get(urljoin(baseurl, path), headers=headers, params=querystring, verify=False)
jsonarray = result.json()
jsonarray['date_time'] = now
with open("/opt/bamboo-agents/akamai/logs/outputfile", "a") as outfile:
json.dump(jsonarray, outfile)
然而,请注意结果文件不是有效的JSON。一个JSON文档是一个单一的结构,而不是一组单独的结构。
英文:
You're calling it "jsonarray", but it is neither JSON nor is it an array. It is a simple Python dictionary.
now = datetime.datetime.now().isoformat(sep=" ", timespec="seconds")
result = session.get(urljoin(baseurl, path), headers=headers, params=querystring, verify=False)
jsonarray = result.json()
jsonarray['date_time'] = now
with open("/opt/bamboo-agents/akamai/logs/outputfile", "a") as outfile:
json.dump( jsonarray, outfile )
HOWEVER, be aware that the result file is not valid JSON. A JSON document is a single structure, not a set of separate structures.
答案2
得分: 0
这是实现我期望输出的代码:
result = session.get(urljoin(baseurl, path), headers=headers, params=querystring, verify=False)
ruletree = result.json()
now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
new_d = {"date_time": now, **ruletree}
with open('/opt/bamboo/logs/outputfile', 'a') as outfile:
json.dump(new_d, outfile)
print(file=outfile)
英文:
Here is the code to achieve my desired output
result = session.get(urljoin(baseurl, path), headers=headers, params=querystring, verify=False)
ruletree = result.json()
now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
new_d = {"date_time": now, **ruletree}
with open('/opt/bamboo/logs/outputfile', 'a') as outfile:
json.dump(new_d, outfile)
print(file=outfile)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论