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 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() |
I'm glad to read that you find this snippet useful :-)
Brilliant - perfect solution here too. Many thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
works great and a huge help for me.
I wanted to put into AWS via apigateway/lambda.
changed the file slightly:
import awsgi
from flask_cors import CORS
from flask import Flask,request
`replaced if name_ =='main'... ,
with a handler + awsgi
def handler(event, context):
return awsgi.response(app, event, context)
`
deployed and worked first time.