unable to fetch json attribute in POST request
<p>This Json is received as a POST request. Now
I want to get value of <code>text</code> key of each entry in <code>actions</code> array</p>

<p>I am using Python's Bottle to receive the request.
to fetch the value of required attribute, I did this</p>

<pre><code>word = request.forms.get('[attachments][actions][0][text]')
</code></pre>

<p>But this doesn't print required value. </p>

<pre><code>{
  "attachments": [
    {
      "title": "XYZ",
      "title_link": "EDWE",
      "text": "dxjhvgebndm",
      "fields": [
        {
          "title": "Food",
          "value": "$20",
          "short": true
        }
      ],
      "actions": [
        {
          "name": "chess",
          "text": "Approve",
          "type": "button",
          "value": "chess",
          "style": "primary"
        },
        {
          "name": "maze",
          "text": "Decline",
          "style": "danger",
          "type": "button",
          "value": "maze"
        },
        {
          "name": "war",
          "text": "More details",
          "style": "default",
          "type": "button",
          "value": "war",
          "confirm": {
            "title": "Are you sure?",
            "text": "Would you like to see more details of your expense?",
            "ok_text": "Yes",
            "dismiss_text": "No"
          }
        }
      ],
      "image_url": "",
      "thumb_url": "https://i.imgsafe.org/cf40eef.png",
      "footer": "fghj",
      "footer_icon": "https://i.imgsafe.org/cf2e0eef.png",
      "ts": 1475057533
    }
  ]
}
</code></pre>

<p>Note: I am receiving complete JSON, the problem is in fetching correct attribute.</p>

<p><strong>EDIT</strong>
Through this i am receiving POST request</p>

<pre><code>import json
from bottle import route, run, request
import urllib

@route('/ocr_response', method='POST')
def ocr_response():
    body = request.body.read()
    word = request.forms.get('[attachments][actions][0][text]')
    print word
    print body


if __name__ == "__main__":
    run(host='0.0.0.0', port=80, debug=True)
</code></pre>

<p>That's not how you access items in a dictionary at all.</p>

<p>Firstly, the JSON data is available via <code>request.json</code>. Secondly, I'm not sure what you're doing with that string you're passing to get, but you need to use normal dictionary/array syntax. And thirdly, attachments is a list just like actions, so you'd need to add an index there too.</p>

<pre><code>request.json['attachments'][0]['actions'][0]['text']
</code></pre>

python json post bottle