In my python script, I'm trying to loop through a text file containing domain names, and fill them to my JSON request body. The correct format required for the API call is
payload = {
"threatInfo": {
"threatEntries": [
{"url": "http://malware.wicar.org/"}, {"url": "http://urltocheck2.org"},
]
}
}
The variable I'm using to replicate this is called mystring
domain_list_formatted = []
for item in domain_list:
domain_list_formatted.append("""{"url": """ + '"{}"'.format(item) + "},")
domain_list_formatted_tuple= tuple(domain_list_formatted)
mystring = ' '.join(map(str, (domain_list_formatted_tuple)))
Printing mystring gets me the results I need to pass to the payload variable
{"url": "http://malware.wicar.org/"},
{"url": "http://www.urltocheck2.org/"},
However, I want to loop this, so I add the following loop
for item in domain_list_formatted_tuple:
printcorrectly = ' '.join(map(str, (domain_list_formatted_tuple)))
payload["threatInfo"]["threatEntries"] = [printcorrectly]
And this is the result:
['{"url": "http://malware.wicar.org/"}, {"url": "http://www.urltocheck2.org/"}']
The single quotes on the outside of the bracket completely throw it off. How is the for loop modifying or encoding the payload in a way that's creating this issue? Your help would be greatly appreciated.