1

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.

Vuotto
  • 11
  • 2
  • I would not build json text in code. I would build a Python data structure and then convert that using the json library. See this answer for an example: https://stackoverflow.com/a/9952774/1317713 https://docs.python.org/3/library/json.html – Leonid Mar 20 '19 at 21:19
  • 1
    Eh... your code says `' '.join(` so you are joining all your entries into a single string. And that string ends up quoted because, well, it is a string. And you have `[printcorrectly]` so you end up with a string inside a list. And that is exactly the value you get. What do you expect to get? – Mad Wombat Mar 20 '19 at 21:26
  • Converting it to a string was, to my knowledge, the only way for it to retain the format necessary to pass to the API. I'm not fluent in Python, but I was under the impression that there would be way to further format the string within the for loop. – Vuotto Mar 20 '19 at 22:40

1 Answers1

0

Your code:

for item in domain_list_formatted_tuple:
   printcorrectly = ' '.join(map(str, (domain_list_formatted_tuple)))   
   payload["threatInfo"]["threatEntries"] = [printcorrectly]

should probably be:

for item in domain_list_formatted_tuple:
   printcorrectly = ' '.join(map(str, (domain_list_formatted_tuple)))   
   payload["threatInfo"]["threatEntries"] = printcorrectly

without the brackets around printcorrectly

If you have :

a = ['xxx']
print(a)

You will get output ['xxx'] with brackets and quotation marks.

Granny Aching
  • 1,295
  • 12
  • 37
  • Thank you for your insight, I do appreciate the help! I did originally have it without the brackets, but doing so still prints the payload with the quotes, and without the brackets. The brackets are necessary for the structure of the payload. It's just the single quotes outside of the curly braces, and I can't seem to figure it out. – Vuotto Mar 20 '19 at 22:38