0

I am trying to build a JSON in Python. I want to send it to Slack.

xxdata = []
xxdata.append("Option A")
xxdata.append("Option B")
data=[]
for xx in xxdata:
    item = {"text": {
                        "type": "plain_text",
                        "text": xx,
                        "emoji": True
                    }}
    data.append(dict(item))
jsonData=json.dumps(data)

This is how I am sending it to slack:

        {
        "type": "section",
        "block_id": "Settings1",
        "text": {
            "type": "mrkdwn",
            "text": ":gear: *MAIN*\nSelect your main group"
        },
        "accessory": {
            "type": "static_select",
            "placeholder": {
                "type": "plain_text",
                "text": "Option A",
                "emoji": True
            },
            "options": jsonData,
            "action_id": "NotificationSelect"
        }

However, when it is sent to Slack - I am getting additional quotation marks before and after the option data:

    {
     "type": "section", 
     "block_id": "Settings1", 
     "text": 
           {
               "type": "mrkdwn", 
               "text": ":gear: *MAIN*\nSelect your main group"}, 
     "accessory": {
               "type": "static_select",
               "placeholder": {
                       "type": "plain_text", 
                       "text": "Option A", 
                       "emoji": true}, 
     "options": "[
                 {
                  "text": 
                        {"type": "plain_text", 
                         "text": "Option A",  
                         "emoji": true}
                 },
                 {"text": 
                        {"type": "plain_text", 
                         "text": "Option B", 
                         "emoji": true}
                 }
  ]", 
       
 "action_id": "NotificationSelect"}},

This causing Slack to fail. what am I doing wrong? If I am removing these quotation marks everything works fine.

2 Answers 2

1

jsonData=json.dumps(data) is making a string from your list. Just use data directly instead of jsonData and you should be fine.

Sign up to request clarification or add additional context in comments.

Comments

0

JSON data is wrapped inside "". That is why you are having two quotation marks for the value of the option key.

Try skipping the step where you do:

jsonData=json.dumps(data)

As such, you will have this:

xxdata = []
xxdata.append("Option A")
xxdata.append("Option B")
data=[]
for xx in xxdata:
    item = {"text": {
                        "type": "plain_text",
                        "text": xx,
                        "emoji": True
                    }}
    data.append(dict(item))

Then:

    {
    "type": "section",
    "block_id": "Settings1",
    "text": {
        "type": "mrkdwn",
        "text": ":gear: *MAIN*\nSelect your main group"
    },
    "accessory": {
        "type": "static_select",
        "placeholder": {
            "type": "plain_text",
            "text": "Option A",
            "emoji": True
        },
        "options": data,  # replace json with dict
        "action_id": "NotificationSelect"
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.