在API调用的JSON输出中添加时间戳。

huangapple go评论100阅读模式
英文:

Add timestamp to API call JSON output

问题

  1. {"date_time": "2023-06-26 12:41:49", "account": "act_F-AC-1234", "contract": "ctr_F-1234"}
  2. {"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:

  1. now = datetime.datetime.now().isoformat(sep=" ", timespec="seconds")
  2. result = session.get(urljoin(baseurl, path), headers=headers, params=querystring, verify=False)
  3. jsonarray = result.json()
  4. json_object = json.dumps(jsonarray)
  5. #writing to outputfile
  6. with open("/opt/bamboo/logs/outputfile", "a") as outfile:
  7. outfile.write(now + json_object + "\n")

current output looks like this

  1. 2023-06-26 12:41:49{"account": "act_F-AC-1234", "contract": "ctr_F-1234"}
  2. 2023-06-26 12:42:49{"account": "act_F-AC-5678", "contract": "ctr_F-5678"}

my desired output should be

  1. {"date_time": "2023-06-26 12:41:49", "account": "act_F-AC-1234", "contract": "ctr_F-1234"}
  2. {"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

  1. {
  2. "account": "act_F-AC-1234",
  3. "contract": "ctr_F-1234",
  4. "rules": {
  5. "name": "default",
  6. "children": [
  7. {
  8. "name": "Route to origin",
  9. "children": [],
  10. "behaviors": [
  11. {
  12. "name": "origin",
  13. "options": {
  14. "originType": "CUSTOMER",
  15. "hostname": "elb.test.gateway.origin.com",

thanks in advance

答案1

得分: 1

你将其称为"jsonarray",但它既不是JSON,也不是一个数组。它是一个简单的Python字典。

  1. now = datetime.datetime.now().isoformat(sep=" ", timespec="seconds")
  2. result = session.get(urljoin(baseurl, path), headers=headers, params=querystring, verify=False)
  3. jsonarray = result.json()
  4. jsonarray['date_time'] = now
  5. with open("/opt/bamboo-agents/akamai/logs/outputfile", "a") as outfile:
  6. 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.

  1. now = datetime.datetime.now().isoformat(sep=" ", timespec="seconds")
  2. result = session.get(urljoin(baseurl, path), headers=headers, params=querystring, verify=False)
  3. jsonarray = result.json()
  4. jsonarray['date_time'] = now
  5. with open("/opt/bamboo-agents/akamai/logs/outputfile", "a") as outfile:
  6. 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

这是实现我期望输出的代码:

  1. result = session.get(urljoin(baseurl, path), headers=headers, params=querystring, verify=False)
  2. ruletree = result.json()
  3. now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
  4. new_d = {"date_time": now, **ruletree}
  5. with open('/opt/bamboo/logs/outputfile', 'a') as outfile:
  6. json.dump(new_d, outfile)
  7. print(file=outfile)
英文:

Here is the code to achieve my desired output

  1. result = session.get(urljoin(baseurl, path), headers=headers, params=querystring, verify=False)
  2. ruletree = result.json()
  3. now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
  4. new_d = {"date_time": now, **ruletree}
  5. with open('/opt/bamboo/logs/outputfile', 'a') as outfile:
  6. json.dump(new_d, outfile)
  7. print(file=outfile)

huangapple
  • 本文由 发表于 2023年6月26日 10:54:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定