how to use only strings to pull json data

Question:

So I keep running into a issue where json is asking to use a int to find pieces of data in a json response. Below is the code that works though the issue is i want to print every ‘name’ in the json though id have to change the [0] to 1 and then 2 ect. I tried to increment it though that ran into issue too. This could be just me overlooking something but let me know, thanks.

def BaseTesting(TarLink):
    PayloadToSend = {
        
    }
    HeadersToSend = { # make sure to change your token at least once every 30 mins
        'authorization': '',
        'user-agent': ''
    }
    ReqForFriends = requests.post(TarLink, headers=HeadersToSend, data=PayloadToSend).text

    LoadedJSONData = json.loads(ReqForFriends)
    print(LoadedJSONData['friends'][0]['name'])

BaseTesting(TarLink="")

JSON

{
   "friends":[
      {
         "name":"test1",
         "user_id":"1132",
         "type":2,
         "display":"true",

      },
      {
         "name":"test2",
         "user_id":"2341",
         "type":1,
         "display":"true",

      },
      {
         "name":"test3",
         "user_id":"1234",
         "type":2,
         "display":"true",

      },
}
Asked By: Kuro

||

Answers:

it seems to work properly

LoadedJSONData = {
   "friends":[
      {
         "name":"test1",
         "user_id":"1132",
         "type":2,
         "display":"true",

      },
      {
         "name":"test2",
         "user_id":"2341",
         "type":1,
         "display":"true",

      },
      {
         "name":"test3",
         "user_id":"1234",
         "type":2,
         "display":"true",

      },
    ]
}


for i in range(0,3):
    print(LoadedJSONData['friends'][i]['name']) #test1 test2 test3
Answered By: Dmitriy Neledva
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.