Array in a json

I am creating a python project who are working with an api, the api return an json like this:

    "1": "2018-10-13T08:28:38.809469028Z",
  "result": [
      {
        "id":3027531,
        "created_at":"2018-10-13T08:20:38.809469028Z",
        "date":"2018-10-13T08:19:38Z",
        "text":"banana",
      }
  ],

I can get 1, but i can't get text in result, can someone help me?

I tried:

response.json()['result']['text']

response.json()['result'].text

response.json().result[0].text

Solution 1:

Try this

response_json = response.json()
response_json["result"][0]["text"]

The value for result is a list of dictionaries. We take the first item in that list, and then we ask for the value of text.

Be aware that this assumes that response_json["result"] has at least one item. If it is empty, you will get an IndexError. You should probably check the length of response_json["result"] before using it. Here is an example of how this could be done:

response_json = response.json()
if response_json["result"]:
    response_json["result"][0]["text"]
else:
    print("result is empty")