我想将此BASH代码转换为python脚本:
curl -XN -u user:Pass -X GET -H "Content-Type: application/json" https://jira.company.com/rest/api/2/search?jql=project='"Technology"+AND+summary~"Remove%20User*"+AND+issuetype="Task"+AND+status!="DONE"' | python -m json.tool > /var/lib/rundeck/1.json
到目前为止我有这个
headers = {
'Content-Type': 'application/json',
}
params = (
('jql', 'project="Technology" AND summary~"Remove User*" AND issuetype="Task" AND status!="DONE"'),
)
response = requests.get('https://jira.company.com/rest/api/2/search', headers=headers, params=params, auth=('user', 'Pass'))
print response
作为回复我得到<Response [200]>
如何将此命令的输出打印到JSON文件(如在bash脚本中)?
使用此代码获取JSON文件但不是JSON结构(作为纯文本):
with open('/var/lib/rundeck/1.json', 'w') as outfile:
outfile.write(response.content)
使用此转换器:
答案 0 :(得分:0)
很简单:
response.raise_for_status() # throw on failure
with open('/var/lib/rundeck/1.json', 'w') as outfile:
outfile.write(str(response.json()))
答案 1 :(得分:0)
您可以尝试以下
from json_tricks.np import dump
with open('response.json','w') as responseFile:
dump({'Data': response },responseFile)
答案 2 :(得分:0)
在Python2中
import requests
import json
url = '<URL>'
payload = open("data.json")
headers = {'content-type': 'application/json'}
request = requests.post(url, data=payload, headers=headers)
with open("output.json", "w") as outfile:
json.dump(output.json(), outfile)