Python JSON GET Request to File

Viewed 46

I'm new to Python but I have an GET Request using requests which is showing me what I want to see, however, how am I able to export it out to a column in CSV?

import requests
url = '<URL>'
response = requests.get(url, verify=False, auth = ('username', 'pass'))
jsonResponse = response.json()
print("NAME")
print(jsonResponse["limits"]['assets'])

The response is: NAME Number (As Assets is nested)

Ideally I'd like to show both NAME and Number in different columns: NAME | Number

My plan is to have multiple requests in the one script which will then spit out a CSV file with the data I'm trying to capture

2 Answers

You can use the CSV module

import csv

with open('test.csv', mode='a') as file:
    w = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    w.writerow(['NAME', jsonResponse["limits"]['assets']])
Related