1

I'm receiving json responses in a loop, an unknown number of responses. The loop is stopped when a HTTP status code of anything other than 200 is returned. Each time through the loop, the response is added to a list.

data = []
x = 1
while True:
    link = "http://sample.com/api/" + x
    r = requests.get(link)
    if r.status_code != 200:
        break
    data.append(json.loads(r.text))

Each json response in a batch will have the same keys, but I don't know if batch to batch they will be the same.

The list ends up like this:

[{'year': '2019', 'month': '1', 'day': '1', 'title': 'Some text', 'image': 'http://sample.com/image1.jpg', 'desc': 'some description of the content'},
 {'year': '2019', 'month': '1', 'day': '2', 'title': 'New Title', 'image': 'http://sample.com/image2.jpg', 'desc': ''}]

How do I convert this to a single dataframe, using the keys in the response as the column names? If I can do it within the loop rather than converting after the fact, I'm fine with that option as well.

tim
  • 879
  • 1
  • 8
  • 26
  • 1
    [This answer](https://stackoverflow.com/a/53831756/4909087) will give you more info than you probably wanted to know, but the short summary of it is: `pd.DataFrame(data)` or `pd.DataFrame.from_dict(data)`. – cs95 Jun 19 '19 at 05:28
  • Thank you. the `from_dict()` method is what I was missing got me the desired results. Searching specifically for json I missed all the dictionary-related questions. – tim Jun 19 '19 at 16:34

0 Answers0