Home > Software design >  Saving API response as a true .JSON file
Saving API response as a true .JSON file

Time:12-03

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.

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 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...

Files -
Screenshot #1. How my JSON data saved and looked, prior to the undocumented code change -

enter image description here

Screenshot #2. How my JSON code looks now, having made some undocumented changes - enter image description here

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.

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. 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).

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?

Ultimately, the objective is to make the api_response as easy to save to a pandas DataFrame as possible.

CodePudding user response:

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()
  • Related