Put the code blocks in order to import the json library. Then convert the string <code>x</code> to a Python dictionary. Then print the value for the <code>“age”</code> key.
import json
---
x = '{ "name":"John", "age":30, "city":"New York"}'
---
y = json.loads(x)
---
y = json(x) #paired
---
print(y["age"])
---
print(y[1]) #paired
Checkpoint15.10.2.
Put the blocks in order to output a Python dictionary <code>x</code> as a JSON string. Watch out for an extra piece of code!
import json
---
x = {
---
"name": "John",
"age": 30,
"city": "New York"
---
}
---
y = json.loads(x) #distractor
---
y = json.dumps(x)
Checkpoint15.10.3.
Put the blocks in order to import the needed module, define the data, read an XML tree from the data, and print the name and email hide attribute value.
import xml.etree.ElementTree as ET
---
data = '''
<person>
<name>Chuck</name>
<phone type="intl">
+1 734 303 4456
</phone>
<email hide="yes" />
</person>'''
---
tree = ET.fromstring(data)
---
print('Name:', tree.find('name').text)
---
print('Attr:', tree.find('email').get('hide'))
Checkpoint15.10.4.
Put the blocks in order to define a JSON encoding that is roughly equivalent to the simple XML from before. It first includes the name and phone number, then the email (which isn't visible). Drag the blocks of statements from the left column to the right column and put them in the right order. Watch your indentation!
Put the blocks in order to convert JSON data for Chuck and Brent to Python then print the number of entries and their data, but the code is mixed up. Drag the blocks of statements from the left column to the right column and put them in the right order. Watch out for three extra pieces of code and indentation!
import json
---
data = '''
---
data = " #distractor
---
[
---
{ "id" : "001",
"x" : "2",
"name" : "Chuck"
} ,
---
{ "id" : "009",
"x" : "7",
"name" : "Brent"
}
---
]'''
---
]" #distractor
---
info = json.loads(data)
---
info = json.dumps(data) #distractor
---
print('User count:', len(info))
---
for item in info:
print('Name', item['name'])
print('Id', item['id'])
print('Attribute', item['x'])
Checkpoint15.10.6.
Put the blocks in order to define the XML for a note. First the recipient is listed, then the sender, heading, and message. There is an extra block that isn't needed in a correct solution.
<note>
---
<note/> #distractor
---
<to>Tove</to>
---
<from>Jani</from>
---
<heading>Reminder</heading>
---
<body>Don't forget me this weekend!</body>
---
</note>
Checkpoint15.10.7.
Put the blocks in order to create a restaurant's menu that lists breakfast items, their price, description, and calorie count. These 3 items should be listed in the order of: Belgian Waffles, French Toast, Homestyle Breakfast.
<breakfast_menu>
---
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
---
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>
Thick slices made from our homemade sourdough bread
</description>
<calories>600</calories>
</food>
---
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>
Two eggs, bacon or sausage, toast, and our ever-popular hash browns
</description>
<calories>950</calories>
</food>
---
</breakfast_menu>
Checkpoint15.10.8.
Put the blocks in order to convert a python dictionary to JSON, then use the sort_keys parameter to specify if the result should be sorted or not. Watch your indentation and make sure closing brackets/braces are in the right order!