简体   繁体   English

将 API 响应保存为 true.JSON 文件

[英]Saving API response as a true .JSON file

Background -背景 -
I am writing some code that will take an API response, write it to a file, and also save it in a pandas DataFrame, and eventually perform some data QC.我正在编写一些代码,它将获取 API 响应,将其写入文件,并将其保存在 pandas DataFrame 中,并最终执行一些数据 QC。

Issue -问题 -
I have a def response_writer(): function, which takes the response of an api call from another function ( def api_call(): ) and saves it using json.dumps . I have a def response_writer(): function, which takes the response of an api call from another function ( def api_call(): ) and saves it using json.dumps . I made some undocumented changes to my code and note that my code used to save the api_response as a.json file, however since making changes to my code, the 'api_response' is now saved in an 'extensionless' file, which although appears to be JSON, looks different when I open the file, to the pre-changes file...我对我的代码进行了一些未记录的更改,并注意到我的代码用于将api_response保存为 .json 文件,但是由于对我的代码进行了更改,“api_response”现在保存在“无扩展名”文件中,虽然看起来是 JSON,当我打开文件时看起来不同,到预更改文件...

Files -文件 -
Screenshot #1 .截图#1 How my JSON data saved and looked, prior to the undocumented code change -在未记录的代码更改之前,我的 JSON 数据如何保存和查看 -

在此处输入图像描述

Screenshot #2.截图#2。 How my JSON code looks now, having made some undocumented changes -我的 JSON 代码现在看起来如何,进行了一些未记录的更改 - 在此处输入图像描述

Current code -当前代码 -
These are my functions, following the undocumented changes -在未记录的更改之后,这些是我的功能-

a.一个。 Function that calls the api, and uses json.load() to save the response in a variable named api_response . Function 调用 api,并使用json.load()将响应保存在名为api_response的变量中。

def api_call():
    url_list = url_constructor()
    for url in url_list:
        response = requests.get(url_list[0], auth = HTTPBasicAuth(key, secret), headers={"My-Firm":"543"})
    api_response = json.loads(response.text)
    return api_response

b.湾。 Function that is supposed to save the api_response as a.JSON file. Function 应该将api_response保存为 .JSON 文件。 However, whilst is appears JSON in the file, it doesn't have the same pretty structure as it did in before the undocumented change (see data.json in 2nd screenshot above).但是,虽然文件中出现了 JSON,但它的结构与未记录更改之前的结构不同(请参见上面第二张屏幕截图中的data.json )。

def response_writer():
    api_response = api_call()
    timestr = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M")
    filename = 'api_response_'+timestr
    with open(filename, 'w', encoding='utf-8') as output_data:
        json.dump(api_response, output_data)
        print("--------------------------------------------\n", "API RESPONSE SAVED:", filename, "\n--------------------------------------------")
response_writer()

Question - Are there any benefits in the api_response being written to a file per Screenshot #1 vs Screenshot #2?问题 -每个屏幕截图 #1 和屏幕截图 #2 将api_response写入文件有什么好处吗?

Ultimately, the objective is to make the api_response as easy to save to a pandas DataFrame as possible.最终,目标是使api_response尽可能容易地保存到 pandas DataFrame 中。

adding the file extension might fix it:添加文件扩展名可能会修复它:

def response_writer():
    api_response = api_call()
    timestr = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M")
    filename = 'api_response_'+timestr+'.json'
    with open(filename, 'w', encoding='utf-8') as output_data:
        json.dump(api_response, output_data)
        print("--------------------------------------------\n", "API RESPONSE SAVED:", filename, "\n--------------------------------------------")
response_writer()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM