I am parsing data through a json file and obtaining multiple URLs, around 50.
below is part of the command I used to pull out just the URLs
results = json.loads(results)
for repo in results:
DevOps_url = (repo['remoteUrl'])
print(DevOps_url)
The output looks something like this:
https://sjdfnlajsdfljhasdfajdfhlkajsdfhajsdfhlajksdfh
https://sjdfnlajsdfljhasdfajdfhlkajsdfhajsdfhlajksdfh
https://sjdfnlajsdfljhasdfajdfhlkajsdfhajsdfhlajksdfh
https://sjdfnlajsdfljhasdfajdfhlkajsdfhajsdfhlajksdfh
https://sjdfnlajsdfljhasdfajdfhlkajsdfhajsdfhlajksdfh
https://sjdfnlajsdfljhasdfajdfhlkajsdfhajsdfhlajksdfh
https://sjdfnlajsdfljhasdfajdfhlkajsdfhajsdfhlajksdfh
https://sjdfnlajsdfljhasdfajdfhlkajsdfhajsdfhlajksdfh
https://sjdfnlajsdfljhasdfajdfhlkajsdfhajsdfhlajksdfh
https://sjdfnlajsdfljhasdfajdfhlkajsdfhajsdfhlajksdfh
https://sjdfnlajsdfljhasdfajdfhlkajsdfhajsdfhlajksdfh
https://sjdfnlajsdfljhasdfajdfhlkajsdfhajsdfhlajksdfh
etc.
How can I grab those URLs and put them all into one list so I can parse through the list for each URL? I have tried the .split() method and that just puts each individual URL into its own list and thats not what I want.
I want it to look something like this ["url","url","url",etc.] in the output.
Any help would be appreciated!
CodePudding user response:
You could use append to add urls from loop to new list:
results = json.loads(results)
all_urls = []
for repo in results:
all_urls.append(repo['remoteUrl'])
Also you could use list comprehension to do the same in one line:
results = json.loads(results)
all_urls = [repo['remoteUrl'] for repo in results]
Then you could iterate over created urls with for loop like so:
for url in all_urls:
print(url)
CodePudding user response:
It could help you:
import json;
file = open("./json.json")
loaded = json.load(file);
file.close()
# List for each value of json's key
list_of_content = []
# Get all keys of the json file and their value
for key in loaded:
list_of_content.append(loaded[key])
for an_value in list_of_content:
#Do something with each value...
print(type(an_value))
print(an_value)
# etc...
