0

I'm trying to get the malicious value from a JSON string.

What's a straight forward way of getting this value? Screenshot and code below.

Pastebin: link

Screenshot:

enter image description here

import json
  
def vt(domain):

  # load json content from file
  with open('vt.txt', 'r') as file:
    content = file.read()

  # convert from sring to json
  content = json.loads(content)

  # read how many engines detect this domain as malicious 
  malicious = content["data"]["attributes"]["last_analysis_stats"]["malicious"]
            
  
vt("pp-alert.com")

Repl.it code: https://repl.it/@MiguelDuarte1/ButteryCanineDonateware

8
  • 2
    How did you get the idea to use [0] in content["data"]["attributes"][0]["malicious"]? I don't see any list in your JSON file. You have to access the correct dictionary key. Commented Oct 17, 2020 at 19:46
  • Also, you're missing ["last_analysis_stats"] Commented Oct 17, 2020 at 19:47
  • Based on the POST link from the bottom. Also tried ´content["data"]["attributes"]["last_analysis_stats"]["malicious"]´ without success. Commented Oct 17, 2020 at 19:47
  • last_analysis_stats doesn't work Commented Oct 17, 2020 at 19:48
  • TypeError: list indices must be integers or slices, not str Commented Oct 17, 2020 at 19:49

2 Answers 2

2
import json
  
def vt(domain):

  # load json content from file
  with open('vt.txt', 'r') as file:
    content = file.read()

  # convert from sring to json
  content = json.loads(content)

  # read how many engines detect this domain as 
  malicious = content['data'][0]['attributes']['last_analysis_stats']['malicious']
  #print(malicious)        
  
vt("pp-alert.com")

This will give you the result.

Note: the JSON in Pastebin link you have provided is different than that you actually use in the vt.txt file.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man! Really worked... thanks for helping a total stranger :-) +1
We're always eager to help. But please make sure, that you give us correct information. As @KokulJose said: This code won't run with the JSON file you provided in the pastebin.
0

I think you missed a field, you don't need [0] since attributes is not an array.
Please try:

malicious = content['data']['attributes']['last_analysis_stats']['malicious']

4 Comments

TypeError: list indices must be integers or slices, not str
@Andre I was able to successfully read the malicious field using Ofek Hod's solution. Is there other JSON input that you are trying to read as well?
This will be the answer if the JSON in Pastebin is the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.