Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python read data from JSON class

Hi all I was facing trouble when trying to read JSON data in python. Here is the JSON format result that I have. The following includes the error that I had occured in the console. The data that i would like to retrieve is response[‘choices’][‘text’]

Error

print(response['choices']['text'])
TypeError: list indices must be integers or slices, not str

Command Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

print(response[‘choices’][‘text’])

JSON Format

{
  "choices": [
    {
      "finish_reason": "length",
      "index": 0,
      "logprobs": null,
      "text": "\n\nThis report discusses work performed during the period January-March. A total of 2,002 evaporation me were performed under contract 12, completed during the second quarter, yielding a total of film samples ranging in size from 1-inch to 1-inch. Processing data showed that the average conversion efficiency was raised by 2 percent. The principal increase was believed to be due to the curious oxide slurry technique developed in the laboratory. A single crystal cell processed in this manner exhibited a nominal efficiency of 9 percent, nearly twice that of a crystal cell prepared prior to electroplating. The spectral transmission curve of the evaporated Al film showed the effect of an optical transmission barrier layer formed by the curious oxide slurry. Thermodynamic optical measurements were discussed, including transmission and UV behavior. A synthetic substrate material was used for solar simulator measurements in order to determine conversion efficiency. Electric measurements metioned briefly includedHall effect and conductivity measurement, added to the usual determine tions of efficiency and rect"
    }
  ],
  "created": 1664503532,
  "id": "cmpl-5vzh6uFm3SZl9UdtXRRJaU2ji39y5",
  "model": "text-davinci-002",
  "object": "text_completion",
  "usage": {
    "completion_tokens": 200,
    "prompt_tokens": 195,
    "total_tokens": 395
  }
}

>Solution :

blhsing is correct: print(response[‘choices’][0][‘text’])

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading