Skip to content
Advertisement

How to organize data from API source to output from Python into Excel?

I’m using requests (see below) to call API source and fetch some data (see below). I’m not sure how to deal with it.

It looks like JSON and/or a dictionary but I have no idea how to process it in Python before exporting to Excel. I understand, I have not imported JSON at this stage but I can.

My code:

JavaScript

Data returned:

JavaScript

Advertisement

Answer

Continuing from your code, you can parse the json string directly with a requests method:

JavaScript

You now have a python object called obj, which is a complex structure of nested dictionaries. That’s very easy to manipulate in python, but it certainly doesn’t look tabular, so it doesn’t make much sense to try to export that to Excel. Also, as noted in the comments, you haven’t indicated what the result should look like.

However, the averages key inside your data does seem tabular, here’s how you can create a dataframe from it and export it to excel:

JavaScript

Note the obj['averages'] in the json.dump call. Also, I used the T function to transpose the dataframe, it seemed more natural (but that’s optional. Here’s what the dataframe looks like:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement