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:
There are two important design principles of writing a recursive function:
- Every recursive function needs a base case that ends the recursion. The base case for the factorial function is when n equals zero. The value for zero factorial is known, and there is no reason to proceed further.
- Every recursive call (when a function calls itself) should operate on a subset of the data it was given. In other words, the purpose of recursion is to divide a problem into smaller sub-problems. The factorial function shown above performs a recursive call with n-1 so the argument gets smaller each time the function is called.
Let’s examine another simple recursive function. How can you use recursion to reverse the order of the characters in a string?