pythonjsonwebhooksendpoint

parsing webhook information to fetch specific parts in python


I have a code that listens for a post to be sent It receives the post as

    {"description": "Test Call", "map_code": "", "details": "", "cross_street": ""}

It will print this using the following

def return_response():
    Dic = request.json;
    data = json.dumps(Dic)
    print(data)

I have also been able to print it using

print(request.json);

To my attempts i have not been able to convert this from the dictionary/lists to specific parts such as

print('description')
print('details')

*** FUll CODE UPDATE

from pydub import AudioSegment
from pydub.playback import play
from flask import Flask, request, Response
from gevent.pywsgi import WSGIServer
import json
app = Flask(__name__)
@app.route('/my_webhook', methods=['POST'])
def return_response():
    Dic = request.json;
    data_str = json.dumps(Dic)
    print(Dic.keys())
    song = AudioSegment.from_wav('alert.wav')
    play(song)
    ## Do something with the request.json data.
    return Response(status=200)
if __name__ == "__main__": app.run(host='0.0.0.0', port=5000)

Thank you for any guidance to come.


Solution

  • Thanks to help from Johnny I was able to work through the problem. The information being sent doesn't have the content/json tag so force must be true. Then there are sub layers to the information such as { unit 1 { unit 2{ unit 3

    To get to the information i needed to call each layer

    data = request.get_json(force=True)
        name = data.get('alert', '')
        name2 = name.get('normalized_message', '')
        discription = name2.get('description', '')
    

    Only then was the final call the post data

    and

    print('discription' 
    

    now returns the information from the post

    Once i could print it i could pass it to other parts of the application.