Skip to content

Instantly share code, notes, and snippets.

@fmaida
Last active December 26, 2023 23:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmaida/faee02b304b0444845703a3d130d928c to your computer and use it in GitHub Desktop.
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
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()
@johnkneedee
Copy link

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.

@fmaida
Copy link
Author

fmaida commented Nov 22, 2022

I'm glad to read that you find this snippet useful :-)

@caver456
Copy link

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