I have a JSON that looks something like this:
[
{
"weburl": "https://google.com/athens",
"location": "Greece"
},
{
"weburl": "https://google.com/rome",
"location": "Italy"
}
...
]
What I want to do is create a function to pass this json into that
I'm having trouble doing this (particularly navigating through key,value and calling urlparse on the value) in Python, and any help would be appreciated!
Thanks.
Since in your example it seems to be a list of dictionaries, and assuming Python-3.x I would suggest you try:
import json
from urllib.parse import urlparse
def f(raw_json):
dict_list = json.loads(raw_json) # assuming raw_json is a string.
# if raw_json is already a parsed json then start here:
for dic in dict_list:
try:
dic['weburl'] = urlparse(dic['weburl']).hostname
except KeyError:
pass
return dict_list