Home > Mobile >  How to parse json request in python?
How to parse json request in python?

Time:08-18

I'm attempting to pull values out of a post request from slack inside of fast api. According to the slack API I should be receiving a json payload. The body I capture appears to be encoded and I'm unsure how to get it into json form.

What I have

async def get_body(request: Request):
    return await request.body()


@app.post("/slack", status_code=200)
async def recieveSlackInteraction(body = Depends(get_body)):
    decode = body.decode("utf-8")
    decode2 = urllib.parse.unquote(decode)
    with open("data_file.json", "w") as write_file:
        json.dump(decode2.strip('"'), write_file)

    
    return 

The above gives me this. How do I turn this into a json object I can access normally?

"payload={\"type\":\"block_actions\",\"user\":{\"id\":\"UKLSJDHFUMR\",\"username\":\"user\",\"name\":\"user\",\"team_id\":\"TDFSDSSDJ\"},\"api_app_id\":\"A0SDFSDFDSFL2U\",\"token\":\"SDFDSFSDFSDFYgk\",\"container\":{\"type\":\"message\",\"message_ts\":\"SDFSDFSDFDSF19\",\"channel_id\":\"GSDFSDFSDFSDFPB\",\"is_ephemeral\":false},\"trigger_id\":\"SDFSDFSDFDSFSDFSDFSDF\",\"team\":{\"id\":\"SDFSDFSDFSDFSDF\",\"domain\":\"SDFSDFSDF\"},\"enterprise\":null,\"is_enterprise_install\":false,\"channel\":{\"id\":\"SDFSDFSDF\",\"name\":\"privategroup\"},\"message\":{\"type\":\"message\",\"subtype\":\"bot_message\",\"text\":\"This content can't be displayed.\",\"ts\":\"SDFSDFSDF\",\"bot_id\":\"SDFSDFSDFDSF\",\"blocks\":[{\"type\":\"section\",\"block_id\":\"bOsst\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"<http:\\/\\/SDFSDF|SDFSDFSDF>\",\"verbatim\":false}},{\"type\":\"actions\",\"block_id\":\"sRgge\",\"elements\":[{\"type\":\"button\",\"action_id\":\"actionId-0\",\"text\":{\"type\":\"plain_text\",\"text\":\"Click Me\",\"emoji\":true},\"style\":\"danger\",\"value\":\"REBOOT\"}]}]},\"state\":{\"values\":{}},\"response_url\":\"https:\\/\\/hooks.slack.com\\/actions\\/SDFSDF\\/SDFSDFSDF\\/SDFSDFSDFDSFDF\",\"actions\":[{\"action_id\":\"actionId-0\",\"block_id\":\"sRgge\",\"text\":{\"type\":\"plain_text\",\"text\":\"Click Me\",\"emoji\":true},\"value\":\"REBOOT\",\"style\":\"danger\",\"type\":\"button\",\"action_ts\":\"1ASDASDASD"}]}"

CodePudding user response:

Regex would be a good way (which I will code up later). But basically you need to fix up the response into a valid json:

import json

jsonStr = '''"payload={\"type\":\"block_actions\",\"user\":{\"id\":\"UKLSJDHFUMR\",\"username\":\"user\",\"name\":\"user\",\"team_id\":\"TDFSDSSDJ\"},\"api_app_id\":\"A0SDFSDFDSFL2U\",\"token\":\"SDFDSFSDFSDFYgk\",\"container\":{\"type\":\"message\",\"message_ts\":\"SDFSDFSDFDSF19\",\"channel_id\":\"GSDFSDFSDFSDFPB\",\"is_ephemeral\":false},\"trigger_id\":\"SDFSDFSDFDSFSDFSDFSDF\",\"team\":{\"id\":\"SDFSDFSDFSDFSDF\",\"domain\":\"SDFSDFSDF\"},\"enterprise\":null,\"is_enterprise_install\":false,\"channel\":{\"id\":\"SDFSDFSDF\",\"name\":\"privategroup\"},\"message\":{\"type\":\"message\",\"subtype\":\"bot_message\",\"text\":\"This content can't be displayed.\",\"ts\":\"SDFSDFSDF\",\"bot_id\":\"SDFSDFSDFDSF\",\"blocks\":[{\"type\":\"section\",\"block_id\":\"bOsst\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"<http:\\/\\/SDFSDF|SDFSDFSDF>\",\"verbatim\":false}},{\"type\":\"actions\",\"block_id\":\"sRgge\",\"elements\":[{\"type\":\"button\",\"action_id\":\"actionId-0\",\"text\":{\"type\":\"plain_text\",\"text\":\"Click Me\",\"emoji\":true},\"style\":\"danger\",\"value\":\"REBOOT\"}]}]},\"state\":{\"values\":{}},\"response_url\":\"https:\\/\\/hooks.slack.com\\/actions\\/SDFSDF\\/SDFSDFSDF\\/SDFSDFSDFDSFDF\",\"actions\":[{\"action_id\":\"actionId-0\",\"block_id\":\"sRgge\",\"text\":{\"type\":\"plain_text\",\"text\":\"Click Me\",\"emoji\":true},\"value\":\"REBOOT\",\"style\":\"danger\",\"type\":\"button\",\"action_ts\":\"1ASDASDASD"}]}"'''
jsonStr = jsonStr.split('"payload=')[-1].replace('\"','"').rsplit('"', 1)[0]

jsonData = json.loads(jsonStr)

Using Regex:

import re
import json

jsonStr = '''"payload={\"type\":\"block_actions\",\"user\":{\"id\":\"UKLSJDHFUMR\",\"username\":\"user\",\"name\":\"user\",\"team_id\":\"TDFSDSSDJ\"},\"api_app_id\":\"A0SDFSDFDSFL2U\",\"token\":\"SDFDSFSDFSDFYgk\",\"container\":{\"type\":\"message\",\"message_ts\":\"SDFSDFSDFDSF19\",\"channel_id\":\"GSDFSDFSDFSDFPB\",\"is_ephemeral\":false},\"trigger_id\":\"SDFSDFSDFDSFSDFSDFSDF\",\"team\":{\"id\":\"SDFSDFSDFSDFSDF\",\"domain\":\"SDFSDFSDF\"},\"enterprise\":null,\"is_enterprise_install\":false,\"channel\":{\"id\":\"SDFSDFSDF\",\"name\":\"privategroup\"},\"message\":{\"type\":\"message\",\"subtype\":\"bot_message\",\"text\":\"This content can't be displayed.\",\"ts\":\"SDFSDFSDF\",\"bot_id\":\"SDFSDFSDFDSF\",\"blocks\":[{\"type\":\"section\",\"block_id\":\"bOsst\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"<http:\\/\\/SDFSDF|SDFSDFSDF>\",\"verbatim\":false}},{\"type\":\"actions\",\"block_id\":\"sRgge\",\"elements\":[{\"type\":\"button\",\"action_id\":\"actionId-0\",\"text\":{\"type\":\"plain_text\",\"text\":\"Click Me\",\"emoji\":true},\"style\":\"danger\",\"value\":\"REBOOT\"}]}]},\"state\":{\"values\":{}},\"response_url\":\"https:\\/\\/hooks.slack.com\\/actions\\/SDFSDF\\/SDFSDFSDF\\/SDFSDFSDFDSFDF\",\"actions\":[{\"action_id\":\"actionId-0\",\"block_id\":\"sRgge\",\"text\":{\"type\":\"plain_text\",\"text\":\"Click Me\",\"emoji\":true},\"value\":\"REBOOT\",\"style\":\"danger\",\"type\":\"button\",\"action_ts\":\"1ASDASDASD"}]}"'''


jsonStr = re.match('\"payload=({.*})', jsonStr).group(1)
jsonData = json.loads(jsonStr)
  • Related