sds2019
sds2019 copied to clipboard
Saving API CSV-response in a JSON-file
Hi, we talked with one of the TA's who advised us to chose CSV as the format when choosing the settings of the GET-call in the Danmarks Statisitik API. (JSON was not in the list of available settings). How do we transform the CSV-format to JSON format, which is necessary to complete 3.3.2? Or should we change the link-constructor in 3.3.1?
First, we created the function, that gets the link for specific parametres (sorry, its not perfect):
def construct_link(table_id, lst): url = "https://api.statbank.dk/v1/data/" + table_id + "/CSV?" for element in lst: url += element + "&" return url[:-1]
This is the code for 3.2.2, where we with no sucess tried to use the requests module on csv format:
`import requests import csv
response = requests.get(construct_link("FOD",['tid=*',"barnkon=p"])) response_csv = response.csv()
with open('my_csv.csv', 'w') as csv: csv.write(response_csv)`
If you choose the csv format you will not be able to save the file as a json file. The idea behind the exercise is to retrieve a json file, and use the json library to save the file to your file system
Simply change the "CSV?" to "JSONSTAT?".
Since we were told to get CSV, we thought that the JSONSTAT was not suitable - just like Java and Javascript is not the same. We will use JSONSTAT instead now. Thanks