FAQ: Overview of Data Acquisition Methods - Converting JSON to CSV

This community-built FAQ covers the “Converting JSON to CSV” exercise from the lesson “Overview of Data Acquisition Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

[Beta] Data Science: Foundations

FAQs on the exercise Converting JSON to CSV

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

This page also does not seem to be working correctly. I entered the proper code, can see the data present in the .csv file, but the page does not recognize it as correct. This is getting really frustrating.

EDIT - It turns out that the syntax presented in the discussion for handling files is incorrect. It should be:

with open(…) as file:

If you have the solution presented to you (not the hint, that is also incorrect), this is the format used.

Thanks soltzt, full code:

import requests
import csv

r = requests.get(‘https://api.census.gov/data/2020/acs/acs5?get=NAME,B08303_001E&for=state:*’)

r_json = r.json()

with open(‘commute_data.csv’, mode=‘w’, newline=’’) as file:

writer = csv.writer(file)

writer.writerows(r_json)

Currently you can not even complete the first step of this section without commenting out the “r_json = r.json()”, as such

import requests
import csv

r = requests.get('https://api.census.gov/data/2020/acs/acs5?get=NAME,B08303_001E,B08303_013E&for=county:*&in=state:36')

#r_json = r.json()

If you do not comment out that line, the step will fail with the error:

Output:
Traceback (most recent call last):
  File "script.py", line 6, in <module>
    r_json = r.json()
  File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Which makes the step be unable to completed.

(although this may also be due to an API limit from census.gov as the prior module was running into issues with too many calls being made)

1 Like

Looking further, it runs properly if you change the included

r_json = r.json()

to

r_json = r.json

This is more a question for the curriculum designers: Why get us set up with Jupyter notebook and then go back to doing everything on the Codecademy site? I am paralleling the coding on Jupyter but cannot proceed further as I do not know how to create a .csv file on Jupyter that is associated with the initial code path. I am currently researching how to do so in the forums here and web search/handbook.

Found the answer on StackOverflow:
Go to Jupyter home page, create a text file then save it with the desired name and the extension .csv

I was able to get the API key for unlimited access to the census data. The problem seems to be download limits. I was getting the following message. You’ve exceeded the limit for queries without a key. If you wish to
continue using the Census Data API please
sign up for a key.



So i signed up for a key. Hope this helps someone