I am trying to parse a json input that is basically a dictionary within a dictionary. From suggestions and input from others, this seems to work but I have no idea how I can troubleshoot it to understand it better.
I tried returning myprint, but it doesn’t really show me how it is iterating through the nested dictionary.
#!/usr/bin/env python3 def myprint(d, apistring, url): for k, v in d.items(): if isinstance(v, dict): **myprint(v, apistring, url)** else: if apistring in v: print("{0}{1}".format(url, v)) def main(): apistring = "/api/" url = "https://example.com" input_text = {"country": {"china": "/api/v1/china/", "canada": "/api/v1/china/canada/"}, "continent": {"asia": "/api/v1/asia/", "africa": "/api/v1/asia/africa/"}, "islands": {"madagascar": "/api/v1/madagascar/", "palau": "/api/v1/madagascar/palau/"}} myprint(input_text, apistring, url) if __name__ == '__main__': main()
Output:
- https://example.com/api/v1/china/
- https://example.com/api/v1/china/canada/
- https://example.com/api/v1/asia/
- https://example.com/api/v1/asia/africa/
- https://example.com/api/v1/madagascar/
- https://example.com/api/v1/madagascar/palau/