0

I would like to print specific data in a JSON but I get the following error:

Traceback (most recent call last):
  File "script.py", line 47, in <module>
    print(link['data.file.url.short'])
TypeError: 'int' object has no attribute '__getitem__'

Here is the JSON:

{ 
   "status":true,
   "data":{ 
      "file":{ 
         "url":{ 
            "full":"https://anonfile.com/y000H35fn3/yuh_txt",
            "short":"https://anonfile.com/y000H35fn3"
         },
         "metadata":{ 
            "id":"y000H35fn3",
            "name":"yuh.txt",
            "size":{ 
               "bytes":0,
               "readable":"0 Bytes"
            }
         }
      }
   }
}

I'm trying to get data.file.url.short which is the short value of the url

Here is the script in question:

post = os.system('curl -F "file=@' + save_file + '" https://anonfile.com/api/upload')
link = json.loads(str(post))

print(link['data.file.url.short'])

Thanks

Alan S
  • 93
  • 1
  • 9

3 Answers3

2

Other than os.system() return value mentioned by @John Gordon I think correct syntax to access data.file.url.short is link['data']['file']['url']['short'], since json.loads returns dict.

user12036762
  • 111
  • 3
1

os.system() does not return the output of the command; it returns the exit status of the command, which is an integer.

If you want to capture the command's output, see this question.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
1

You are capturing the return code of the process created by os.system which is an integer.

Why dont you use the request class in the urllib module to perform that action within python?

import urllib.request
import json

urllib.request.urlretrieve('https://anonfile.com/api/upload', save_file)
json_dict = json.load(save_file)
print(json_dict['data']['file']['url']['short'])  # https://anonfile.com/y000H35fn3

Or if you don't need to save the file you can use the requests library:

import requests

json_dict = requests.get('https://anonfile.com/api/upload').json()
print(json_dict['data']['file']['url']['short'])  # https://anonfile.com/y000H35fn3
Rafaelars
  • 73
  • 5