Search code examples
pythonjsonfunctionparsingurlparse

Creating function to parse JSON for particular key, then apply urlparse on its value


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.


Solution

  • 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