| 9 | # Function to convert a CSV to JSON |
| 10 | # Takes the file paths as arguments |
| 11 | def make_json(csv_file, token, ep): |
| 12 | |
| 13 | # Open a csv reader called DictReader |
| 14 | with open(csv_file, encoding='utf-8') as csvf: |
| 15 | csvReader = csv.DictReader(csvf) |
| 16 | |
| 17 | # Convert each row into a dictionary |
| 18 | # and add it to data |
| 19 | for row in csvReader: |
| 20 | message = json.dumps({ |
| 21 | "events": [ row ] |
| 22 | }) |
| 23 | |
| 24 | print(message) |
| 25 | |
| 26 | headers = { |
| 27 | "Accept": "application/json", |
| 28 | "Content-Type": "application/json", |
| 29 | "Authorization": "Bearer {0}".format(token) |
| 30 | } |
| 31 | |
| 32 | url = "https://{0}.api.decodable.co{1}".format(os.getenv("ACCOUNT"), ep) |
| 33 | |
| 34 | response = requests.post(url, headers=headers, data=message) |
| 35 | |
| 36 | print(response.text) |
| 37 | |
| 38 | if __name__ == "__main__": |
| 39 | |