Home > front end >  how to use only strings to pull json data
how to use only strings to pull json data

Time:09-26

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",

      },
}

CodePudding user response:

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
  • Related