Last active
December 26, 2023 23:21
-
-
Save fmaida/faee02b304b0444845703a3d130d928c to your computer and use it in GitHub Desktop.
How to fetch data from a Jotform webhook with Python v3.6+ and Flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from flask import Flask, request | |
app = Flask(__name__) | |
def extract_jotform_data(): | |
output = {} | |
form_data = request.form.to_dict() | |
if form_data.get("rawRequest"): | |
for key, value in json.loads(form_data["rawRequest"]).items(): | |
# Removes the "q<number>_" part from the key name | |
# Instead of "q5_quantity" we want "quantity" as the key | |
temp = key.split("_") | |
new_key = key if len(temp) == 1 else "_".join(temp[1:]) | |
# Saves the item with the new key in the dictionary | |
output[new_key] = value | |
return output | |
@app.route('/', methods=['GET', 'POST']) | |
def hello_world(): | |
jotform = extract_jotform_data() | |
for key, value in jotform.items(): | |
print(f"{key}: {value}") | |
if type(value) is dict: | |
for subkey, subvalue in value.items(): | |
print(f" +------ {subkey}: {subvalue}") | |
return "ok", 200 | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Brilliant - perfect solution here too. Many thanks.