How to check if JSON is empty. If not, append new data

Question:

I am trying to check to see if a JSON is empty within a for loop, and if it is not to append new data. What I am attempting to achieve is the first time around the JSON will be empty, so it needs to create or populate a JSON object. On the 2nd loop and beyond, I need it to append data to the JSON object. Below is my code and output thus far. As you can see for the first 91 loops it is reporting an empty JSON and is overwriting the data rather than appending to it. Can someone please help me?

today = date.today()
json_data = []

for month_start in pd.date_range('2014-01-01', today, freq='MS'):
    month_end = (month_start + MonthEnd(1))
    # calculate unix datetime
    month_start_unix = (month_start - pd.Timestamp("1970-01-01")) // pd.Timedelta('1ms')
    month_end_unix = (month_end - pd.Timestamp("1970-01-01")) // pd.Timedelta('1ms')
    API_ENDPOINT = Link to API server I am attempting to pull from, hidden.
    # Request to Viriciti API
    r = requests.get(url = API_ENDPOINT, headers = Headers)
    # Error message statement
    if r.status_code == 200:
        print('Successfully fetched the data')
    else:
        print(f"there's a {r.status_code} error with your request")
    if len(json_data) == 0:
        # Json data output as a list of dictionaries
        json_data = r.json()
        print('Empty JSON file') 
    else:
        json_data.append(r.json)
        print('Appending to JSON file')       

print(json_data)

Output:

Successfully fetched the data
Empty JSON file
Successfully fetched the data
Empty JSON file
Successfully fetched the data
Empty JSON file
Successfully fetched the data
Empty JSON file
Successfully fetched the data
Empty JSON file
Successfully fetched the data
Empty JSON file
Successfully fetched the data
Empty JSON file
Successfully fetched the data
Empty JSON file
Successfully fetched the data
Empty JSON file
Successfully fetched the data
Empty JSON file
Successfully fetched the data
Empty JSON file
Successfully fetched the data
Empty JSON file
Successfully fetched the data
...
Appending to JSON file
Successfully fetched the data
Appending to JSON file
[{'time': 1625681556815, 'value': 5.4}, {'time': 1625702400000, 'value': 5.6}, {'time': 1625788800000, 'value': 257.4}, {'time': 1625875200000, 'value': 7.2}, {'time': 1625961600000, 'value': 8.2}, {'time': 1626134400000, 'value': 8.2}, {'time': 1626220800000, 'value': 21.6}, {'time': 1626307200000, 'value': 21.6}, {'time': 1626393600000, 'value': 23.6}, {'time': 1626566400000, 'value': 25.8}, {'time': 1626739200000, 'value': 25.8}, {'time': 1626825600000, 'value': 25.8}, {'time': 1626912000000, 'value': 26.4}, {'time': 1627689600000, 'value': 34.4}, <bound method Response.json of <Response [200]>>, <bound method Response.json of <Response [200]>>, <bound method Response.json of <Response [200]>>, <bound method Response.json of <Response [200]>>, <bound method Response.json of <Response [200]>>, <bound method Response.json of <Response [200]>>, <bound method Response.json of <Response [200]>>, <bound method Response.json of <Response [200]>>, <bound method Response.json of <Response [200]>>, <bound method Response.json of <Response [200]>>, <bound method Response.json of <Response [200]>>, <bound method Response.json of <Response [200]>>, <bound method Response.json of <Response [200]>>]
Asked By: Rettro

||

Answers:

import json
with open("fileName.json", "r+") as f:
  try:
    json_data = json.load(f)
    appendInfo(json_data)
    f.seek(0)
    json.dump(json_data, f)
  except ValueError:
    print("Empty File!")

The code above can 1) detect whether a JSON file is empty or not and 2) allow you to append any information to its content and then write them back.
Attention: You have to make sure that the file itself exists, otherwise ‘FileNotFoundError’ will occur.

Answered By: Eric2i

Couple of things here

  1. If the response of your API is a JSON object(not a JSON list/array) which needs to be appended into a list, then this would do the job.
today = date.today()
json_data = []

for month_start in pd.date_range('2014-01-01', today, freq='MS'):
    month_end = (month_start + MonthEnd(1))
    # calculate unix datetime
    month_start_unix = (month_start - pd.Timestamp("1970-01-01")) // pd.Timedelta('1ms')
    month_end_unix = (month_end - pd.Timestamp("1970-01-01")) // pd.Timedelta('1ms')
    API_ENDPOINT = Link to API server I am attempting to pull from, hidden.
    # Request to Viriciti API
    r = requests.get(url = API_ENDPOINT, headers = Headers)
    # Error message statement
    if r.status_code == 200:
        print('Successfully fetched the data')
    else:
        print(f"there's a {r.status_code} error with your request")
    json_data.append(r.json)

print(json_data)
  1. If the response of your API is a JSON Array/List(not a JSON object) who’s contents are to be appended into a list, then this would do the job.
today = date.today()
json_data = []

for month_start in pd.date_range('2014-01-01', today, freq='MS'):
    month_end = (month_start + MonthEnd(1))
    # calculate unix datetime
    month_start_unix = (month_start - pd.Timestamp("1970-01-01")) // pd.Timedelta('1ms')
    month_end_unix = (month_end - pd.Timestamp("1970-01-01")) // pd.Timedelta('1ms')
    API_ENDPOINT = Link to API server I am attempting to pull from, hidden.
    # Request to Viriciti API
    r = requests.get(url = API_ENDPOINT, headers = Headers)
    # Error message statement
    if r.status_code == 200:
        print('Successfully fetched the data')
    else:
        print(f"there's a {r.status_code} error with your request")
    # Simple Append One List With another
    json_data += r.json()
    # Or Unpack contents for json_data and r.json() to a new list
    # json_data = [*json_data, *r.json()]

print(json_data)
Answered By: Sudarshan Thorve

It looks like your API sometimes returns an empty object. You create a list json_data = [] to hold decoded data, but then replace it with the first decoded data. If that data is also empty, then json_data is still empty, and it keeps going until you finally get something that isn’t empty.

But if the goal is to get a list of the data returned, you shouldn’t replace that initial list you created at all. Instead, just check to see if the requested data is empty and append to the original list. BTW, empty lists and dicts are "falsey" so you don’t need to check their lengths.

if r_json:=r.json(): # python 3.8+
    print('Appending to JSON file')
    json_data.append(r_json)
else:
    print('Empty JSON file') 
Answered By: tdelaney
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.