Better Programming

Advice for programmers.

Follow publication

Member-only story

How To Recursively Parse API Responses Using Python

Doron Chosnek
Better Programming
Published in
8 min readMay 14, 2021
empty paint containers and a painting palette on a floor covered in multi-colored paint splatters
Photo by Ricardo Viana on Unsplash

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:

  1. 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.
  2. 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?

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Doron Chosnek
Doron Chosnek

Written by Doron Chosnek

Interested in datacenter and cloud infra as well as cloud native dev. Enjoy hacking scripts to make my life easier.

No responses yet

Write a response