import requests import json # Replace with your JSON URL json_url = "https://download.geofabrik.de/index-v1.json" # Fetch JSON data from the URL response = requests.get(json_url) data = response.json() # List to store extracted values pbf_urls = [] # Recursive function to traverse the JSON def extract_pbf_urls(obj): if isinstance(obj, dict): # Check if 'urls' is in the dictionary and it is a dictionary if "urls" in obj and isinstance(obj["urls"], dict): # Check if 'pbf' key exists and store its value if "pbf" in obj["urls"]: pbf_urls.append(obj["urls"]["pbf"]) # Continue traversal for value in obj.values(): extract_pbf_urls(value) elif isinstance(obj, list): for item in obj: extract_pbf_urls(item) # Run the extraction extract_pbf_urls(data) # Sort the URLs pbf_urls.sort() # Save to a flat JSON file with open("pbf_urls.json", "w") as f: json.dump(pbf_urls, f, indent=4) print("Extracted URLs saved to pbf_urls.json")