Created
June 28, 2023 00:25
-
-
Save italopinto/1c7a13eb3ede17c6e6c96623af422d9e to your computer and use it in GitHub Desktop.
Function to recursevely search for a key and returns it's values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def search_key_in_json(json_obj, search_key): | |
results = [] | |
if isinstance(json_obj, dict): | |
for key, value in json_obj.items(): | |
if key == search_key: | |
results.append(value) | |
elif isinstance(value, (dict, list)): | |
results.extend(search_key_in_json(value, search_key)) | |
elif isinstance(json_obj, list): | |
for item in json_obj: | |
results.extend(search_key_in_json(item, search_key)) | |
return results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment