This forum is now read-only. Please use our new forums at discuss.codecademy.com.

How to use APIs with Python Forum

View Course

714 points
504cfb877bcb3d0002001959_688552999
Submitted by
vit0
over 4 years ago

Result in "Parsing JSON" excersise

When i hit Run i get:

That's correct!
{u'pets': [{u'name': u'Jeffrey', u'species': u'Giraffe'},
{u'name': u'Gustav', u'species': u'Dog'},
{u'name': u'Gregory', u'species': u'Duck'}]}
==> None

I don't know if that is the format the author wanted to be output becouse it should've been printed pretty.


0 votes

permalink

I get:

{u'pets': [{u'name': u'Jeffrey', u'species': u'Giraffe'},
           {u'name': u'Gustav', u'species': u'Dog'},
           {u'name': u'Gregory', u'species': u'Duck'}]}

It looks pretty good to me. Nicely indented and easy to read line breaks. I'm not familiar with json though. Is it supposed to look like something else?

1768 points
5271ecfff10c60e3380032ad_789747621
Submitted by
tp9
over 4 years ago

1 Comment

501c279f28e19000020169ce_91422859 Fletcher Heisler over 4 years ago

Yes, this is the "prettiest" way that the dictionary can be printed without looping over all the items individually to display them.


0 votes

permalink

I don't know...
The output from 'Parsing XML' was a lot nicer. I thought that one won't be so "RAW".
If I'm in mistake, sorry for unneeded question.

714 points
504cfb877bcb3d0002001959_688552999
Submitted by
vit0
over 4 years ago

1 Comment

5271ecfff10c60e3380032ad_789747621 tp9 over 4 years ago

In the XML example they are iterating through the objects and parsing out only the names. In the json example they are printing out the whole json string. Don't quote me on this though. I don't have all the terminology yet but that's what it looks like when I look at the code.


0 votes

permalink

But why appears this u before the property?

1958 points
512244957d637284a6003902_372798797
Submitted by
Guilherme Lunhani
over 4 years ago

1 Comment

Dd27c88f39789e30263e02cd234ea1c7?s=140&d=retro ishiggidydiggidy over 4 years ago

The u before each of the words stands for Unicode, the character encoding JSON uses, which is a different way to store characters (letters, numbers, etc.) than the ASCII way. You can call print on a Unicode string and it will show up just like an ASCII string would in python, e.g., print unicode("foo"), but if you just type unicode("foo") you will see the u in front.


0 votes

permalink

so, the last line in the example is
pprint(pets)

the results is going to show everything inside the pets tag instead of just the name in the XML example.

188 points
Picture
Submitted by
Dave Smith
over 4 years ago