-2

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

  1. Searches for the occurences of key "weburl" in the whole json, and
  2. calls urlparse(value).hostname to replace the string value next to the each of the "weburl" keys to only include hostname, and finally
  3. Return this entire modified json.

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.

bergerg
  • 985
  • 9
  • 23
nm44
  • 45
  • 1
  • 6

1 Answers1

1

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    
bergerg
  • 985
  • 9
  • 23
  • I am getting an error due to the _json.loads(raw_json)_ method call. It says that return_default_decoder_decode(s) is being called and raising a ValueError due to "Extra Data." Any idea what can be done to fix this? I believe my json is a string, so I am not sure why loads is throwing an error. – nm44 Jul 29 '17 at 21:46
  • Is your json a string? i.e. are you reading a json from a file as it was dumped? or is it a "real" json? Without seeing your full code I can only guess this is a valid json. If so, try to comment out the `json.loads` line. – bergerg Jul 29 '17 at 21:51