Created
August 9, 2023 13:01
-
-
Save opplatek/e48d558bd8505d192091772d8e40551b to your computer and use it in GitHub Desktop.
Parse GitLab API raw JSON for projects/groups/subgroups, filter and extract a specific field
This file contains 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
#!/usr/bin/env python3 | |
# | |
# Read GitLab raw JSON and extract ssh url | |
# | |
import sys | |
import json | |
def filter_dicts(list_of_dicts, value_to_check): | |
filtered_list = [item for item in list_of_dicts if not item.get(value_to_check, True)] | |
return filtered_list | |
def extract_value(list_of_dicts, value_to_extract): | |
filtered_list = [item[value_to_extract] for item in list_of_dicts if value_to_extract in item] | |
return filtered_list | |
def main(): | |
input_data = sys.stdin.read() | |
try: | |
list_of_dicts = json.loads(input_data) | |
except json.JSONDecodeError: | |
print("Invalid input format. Expected JSON.") | |
return | |
value_to_check = "archived" | |
filtered_list = filter_dicts(list_of_dicts, value_to_check) | |
value_to_extract = "ssh_url_to_repo" | |
filtered_list = extract_value(filtered_list, value_to_extract) | |
filtered_list.sort() | |
for field in filtered_list: | |
print(field) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment