Skip to content

Instantly share code, notes, and snippets.

@italopinto
Created June 28, 2023 00:25
Show Gist options
  • Save italopinto/1c7a13eb3ede17c6e6c96623af422d9e to your computer and use it in GitHub Desktop.
Save italopinto/1c7a13eb3ede17c6e6c96623af422d9e to your computer and use it in GitHub Desktop.
Function to recursevely search for a key and returns it's values.
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