Parse json fields to variables

I’m trying to parse fields of a json output from an API query.

Example json:

{
   "_id":"611a7b651571d300074b9875",
   "Details":{
      "Source":{
         "Type":"WHOIS servers",
         "URL":"http://tiktea.com.au",
         "NetworkType":"ClearWeb"
      },
      "Type":"Phishing",
      "SubType":"RegisteredSuspiciousDomain",
      "Severity":"Medium"
   },
   "Assignees":[
      
   ],
   "FoundDate":"2021-08-16T14:51:17.747Z",
   "Assets":[
      {
         "Type":"Domains",
         "Value":"iktea.net"
      }
   ],
   "TakedownStatus":"NotSent",
   "IsFlagged":false,
   "Closed":{
      "IsClosed":false
   }
}

I want to parse some fields, and get something like:

URL: http://tiktea.com.au
FoundDate: 2021-08-16T14:51:17.747Z"

so I tried this, but didn’t work: Selecting fields from JSON output

This is my python code:

import requests
import json


#UserID + Api Key
auth = ('USERID','APIKEY')
headers = {    'Content-Type': "application/json",    }


url = "https://api.intsights.com/public/v1/data/alerts/alerts-list?alertType=Phishing
alerts = requests.request("GET", url, auth = auth, headers=headers)

for alertID in alerts.text.split('","'):
  url = "https://api.intsights.com/public/v1/data/alerts/get-alert/"+alertID #<-----This is the request that extract the JSON
  alertDetails = requests.request("GET", url, auth = auth, headers=headers)
  

  dict = alertDetails.text
  url=dict['Assets'][0]['Value'] #<---------Trying to parse the desired value
  print(url)

This is the result:

Traceback (most recent call last):
  File "cefexample.py", line 73, in <module>
    url=dict['Assets'][0]['Value']
TypeError: string indices must be integers

Any suggestion?

Answer

First you need to parse the JSON that comes as response.

data = alertDetails.json() # use convenience method

Then access fields you want like a nested dict.

url = data['Details']['Source'].get('URL')
print (f'Phishing URL: {url}')
found_date = data.get('FoundDate')
print(f'Found date: {found_date}')

This assumes (based on error you show) that Deatails and Source will always be present, ony URL key may be missing. To be on the safe side you can do

url = data.get('Details', {}).get('Source', {}).get('URL')
print (f'Phishing URL: {url}')

or use try/except:

try:
    url = data['Details']['Source']['URL']
except KeyError:
    url = None
print (f'Phishing URL: {url}')