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

How to to access different JSON keys?

I have a JSON file as the following, and I’m trying to access those different keys with Python.

My JSON file format:

{
   "spider":[
      {
         "t":"Spider-Man: No Way Home (2021)",
         "u":"movie\/spider-man-no-way-home-2021",
         "i":"c2NJbHBJYWNtbW1ibW12Tmptb1JjdndhY05FbXZhS1A"
      },
      {
         "t":"Spider-Man: Far from Home (2019)",
         "u":"movie\/spider-man-far-from-home-2019",
         "i":"c2NJbHBJYWNtTGNtdm1qbXZtYm1FRWNtcEV4bWJ4bWJteGo"
      },
      {
         "t":"Spider-Man: Homecoming (2017)",
         "u":"movie\/spider-man-homecoming-2017",
         "i":"c2NJbHBJYWN2TllqbVRibXVjbWJ2d3h2dGNtam1idmM"
      },
      {
         "t":"Spider-Man: Into the Spider-Verse (2018)",
         "u":"movie\/spider-man-into-the-spider-verse-2018",
         "i":"c2NJbHBJYWNtVEVtdnZjbXZtdm1qRWNtYnhtR1VURXZjY3c"
      },
      {
         "t":"Spider-Man (2002)",
         "u":"movie\/spider-man-2002",
         "i":"c2NJbHBJYWNtam1ZanZjbWptakVjbXZtdm1oenh2Y3htSQ"
      },
      {
         "t":"The Spiderwick Chronicles (2008)",
         "u":"movie\/the-spiderwick-chronicles-2008",
         "i":"c2NJbHBJYWNtVG9Oam1qbWJFY21ibWJ2d1BtYm1tbUhj"
      }
   ]
}

How I can access the t, u, and i keys?

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

I tried:

print(json_file['t'])

Nothing helped with the error:

Traceback (most recent call last):
  File "/home/werz/Desktop/trying/programming/nutflix/flask-nutflix/test.py", line 38, in <module>
    print (json_file['t'])
KeyError: 't'

>Solution :

Try indexing for printing like

    print(json_file["spider"][1]["t"])

You can try for loop to print all

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