We have a requirement where we need to run all the json payloads in different files in a directory in our project (airflow).
Each json payload exists in a separate json file in a directory structure json/payloads.
I am trying to write a python function to read all json payloads and send REST calls with payload from these files.
def read_payloads(category):
payload_dict = {}
directory = './json' + "/" + 'payload' + "/" + category
for filename in os.listdir(directory):
if os.path.isfile(filename):
f = open(filename)
payload_dict.update({filename, json.load(f)})
f.close()
payload_dict
I am trying to create a dictionary with key as file names and value as json payload.
I need to pass this dictionary to a python function which will use the json payload from these values and make REST calls one by one.
While trying to execute above function, getting error: No such file or directory:
It seems it is due to absolute / relative path issue.
Also how can I extract the json payload from this dictionary using for loop and make REST call with each payload.
Have a small sample code for REST api.
def server_call(url, token, category):
payload_dict = read_payloads(category)
/*
code to make REST call for one payload
*/
Please advise on fixing above function and how to iterate the payload dictionary to make REST calls by using json payloads from the values of this dictionary.