Member-only story
How To Recursively Parse API Responses Using Python
Parse complicated responses easily
An API response should be predictable. Sometimes you have to interact with an API that is unpredictable, either because it’s poorly designed or because you are utilizing an undocumented (and unsupported) API. I’ve dealt with both of these, so in this article, I will share with you the hurdles faced and how I crossed them. In both cases, I used recursive functions in Python. You can’t use nested loops when you can’t predict how many layers of nesting you’ll need!
A Quick Review of Recursion
For those unfamiliar with writing recursive functions in Python, here is a quick review of the principles. I encourage you to look at these longer and more comprehensive tutorials if you’ve never seen recursion: Thinking Recursively in Python and Recursive Programming.
The most overused example of recursion in every language is calculating the factorial of a number. It’s a silly example for Python developers because Python includes a factorial function in its math library that can outperform anything one could write natively in Python. But the factorial function is simple and easy to follow: