Making API request in Python Question #4

Link: https://www.codecademy.com/journeys/data-scientist-ml/paths/dsmlcj-22-data-science-foundations/tracks/dsmlcj-22-python-fundamentals-for-data-science-part-ii/modules/dsf-data-acquisition-52c834f4-c0ab-482e-b463-0687222c2b26/lessons/overview-data-acquisition-methods/exercises/making-api-request-in-python

Question #4 asks :
"Use the .json() method to access the decoded JSON data and store it in a variable called r_json. Try printing out r_json. Can you see the advantage working with r_json has over r_text?"

I don’t see any differences/advantages between the two. Can anyone explain what it is? It looks the same as one another except for having single quotes (json) vs double quotes (text). Not sure if this has any significance. Thanks!

Howdy!
Could you post your code for us to see? It can be a bit harder to help with out knowing for sure were you’re at :sweat_smile:
Specifically what is assigned to r_text and r_json is what I’m getting at.

Without seeing your code I do have a hunch. Have you tried printing type(r_text) and type(r_json)? Are they both the same? Or could one be easier to work with?

1 Like

Hi, sorry about that. Below is my code:

“”"
import requests

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

r_text = r.text
print(r_text)

r_json = r.json()
print(r_json)
“”"

1 Like

Thank you much! Yep, that’s what I thought!
Try what I recommended earlier, should see a bit of a differnce :wink:

r_text is class str
r_json is class list

Thanks!

2 Likes

When I tried to run this exact same code it gave me an error message and I could not fix it, so I allowed it to substitute in the code they offered from “replace with solution” option and even that code returned the same error message. I did not know what to do but the task box was then checked off so I moved on.

If you scroll up to see what @8-bit-gaming wrote about types of data, one is a string and one is a list type of data. the r_json object is a list, the r_text is string

The list obj. is the type of data you’d want to work with here.