<jupyter_start><jupyter_text>DictionariesNo error trapping<jupyter_code>company = {'Mickey Mouse':'Disney', '<NAME>':'Disney', '<NAME>':'<NAME>', '<NAME>':'<NAME>'}
characterName = input("What's the character's name? ")
print('That character works for ' + company[characterName])<jupyter_output><empty_output><jupyter_text>With error trapping<jupyter_code>company = {'Mickey Mouse':'Disney', '<NAME>':'Disney', '<NAME>':'<NAME>', '<NAME>':'<NAME>'}
characterName = input("What's the character's name? ")
try:
    print('That character works for ' + company[characterName])
except:
    print("I don't know who that character works for.")
print("That's all folks!")<jupyter_output><empty_output><jupyter_text>Try thisA. Using the dictionaries, print the name and price of the item with catalog number `z010`.B. Use a `for` loop to iterate through the list of items. For each item, print its name and price.C. Let the user enter the name of an item. Iterate through the list of items and check each one to see if it matches the name entered by the user. If so, print the price.D. Set a flag named `matched` equal to `False` before the loop. If there is a match, set the value of `matched` equal to `True`. If at the end of the loop there was no match, print a message saying so.<jupyter_code>itemList = ['s049', 'm486', 'z010', 'x428']
itemName = {'s049': 'widget', 'm486': 'poiuyt', 'z010': 'thingamabob', 'x428': 'foobar'}
itemPrice = {'s049': 1.98, 'm486': 14.99, 'z010': 0.49, 'x428': 250.00}

# put your code here<jupyter_output><empty_output><jupyter_text>Lists of dictionariesFirst example<jupyter_code>characters = [{'name':'<NAME>', 'company':'Disney', 'gender': 'male'}, {'name':'<NAME>', 'company':'Disney', 'gender': 'female'}, {'name':'<NAME>', 'company':'Warner Brothers', 'gender': 'male'},  {'name':'<NAME>', 'company':'Hanna Barbera', 'gender': 'male'}, {'name':'WALL-E', 'company':'Pixar', 'gender': 'neutral'}, {'name':'Fiona', 'company':'DreamWorks', 'gender': 'female'}]
print(characters[1]['company'])
print(characters[0]['name'])
print(characters[4]['gender'])<jupyter_output><empty_output><jupyter_text>Second example<jupyter_code>characters = [{'name':'<NAME>', 'company':'Disney', 'gender': 'male'}, {'name':'<NAME>', 'company':'Disney', 'gender': 'female'}, {'name':'<NAME>', 'company':'Warner Brothers', 'gender': 'male'},  {'name':'<NAME>', 'company':'Hanna Barbera', 'gender': 'male'}, {'name':'WALL-E', 'company':'Pixar', 'gender': 'neutral'}, {'name':'Fiona', 'company':'DreamWorks', 'gender': 'female'}]
characterName = input("What's the character's name? ")
for character in characters:
    if character['name'] == characterName:
        print('The character ' + character['name'] + ' works for ' + character['company'] + '.')<jupyter_output><empty_output><jupyter_text>Third example<jupyter_code>characters = [{'name':'<NAME>', 'company':'Disney', 'gender': 'male'}, {'name':'<NAME>', 'company':'Disney', 'gender': 'female'}, {'name':'<NAME>', 'company':'Warner Brothers', 'gender': 'male'},  {'name':'<NAME>', 'company':'Hanna Barbera', 'gender': 'male'}, {'name':'WALL-E', 'company':'Pixar', 'gender': 'neutral'}, {'name':'Fiona', 'company':'DreamWorks', 'gender': 'female'}]
characterName = input("What's the character's name? ")
found = False
for character in characters:
    if character['name'] == characterName:
        found = True
        if character['gender'] == 'male':
            answer = 'He works'
        elif character['gender'] == 'female':
            answer = 'She works'
        else:
            answer = 'They work'
        answer = answer + ' for ' + character['company'] + '.'
        print(answer)
if not(found):
    print("I don't know that character.")<jupyter_output><empty_output><jupyter_text>JSON JSON and PythonFirst example: JSON array nested inside a object (Python list inside a dictionary)<jupyter_code>import json

jsonString = '''
{
  "name":
         [
         "Steve",
         "Steven",
         "Esteban"
         ],
  "fingers":10, 
  "street":"<NAME>"
}
'''

data = json.loads(jsonString)

print(data)
print()
print(data['name'])
print()
print(data['name'][1])
print()
print(data['fingers'])<jupyter_output><empty_output><jupyter_text>Second example: JSON object nested inside an array (Python dictionary inside a list)<jupyter_code>import json
data = json.loads('''
[
    {
        "created_at":"Wed Sep 18 19:50:41 +0000 2019",    
        "text":"The \u201cdigital downloads\u201d tax makes an appearance!",
        "lang":"en"
    },
    {
        "created_at":"Wed Sep 18 19:28:44 +0000 2019",
        "text":"¡No podía sentir las yemas de mis dedos esta mañana, hacía tanto frío",
        "lang":"es"
    },    
    {
        "created_at":"Wed Sep 18 14:08:54 +0000 2019",
        "text":"RT @wnprwheelhouse: @wnprharriet кричать @wnpr !",
        "lang":"ru"
    }
]
''')
print(data)
print()
print(data[1])
print()
print(data[1]['lang'])<jupyter_output><empty_output><jupyter_text>Third example: Example: JSON object nested inside an object (Python dictionary inside a dictionary)<jupyter_code>import json
data = json.loads('''
{
    "in_reply_to_screen_name": null,
    "user": 
            {
            "id": 6253282,
            "id_str": "6253282",
            "name": "<NAME>",
            "screen_name": "cbaskauf",
            "location": "Hartford, CT"
            }
        ,
    "geo": null,
    "coordinates": null
}
''')
print(data)
print()
print(data['user'])
print()
print(data['user']['location'])<jupyter_output><empty_output><jupyter_text>Try this1. JSON about acetylsalicylic acidWrite a script to turn the JSON string into a Python data structure (a dictionary), then print the formula. Don’t forget to import the json module.<jupyter_code>string = '{"id": 2157, "formula": "C_{9}H_{8}O_{4}", "molecularWeight": 180.1574, "commonName": "Aspirin"}'

# put your code here<jupyter_output><empty_output><jupyter_text>2. JSON from the Royal Society of Chemistry APIA. What type of Python object is the value of `dataStructure['results']`?B. How can we refer to the number `2157` as a subcomponent of `datastructure`?C. In this example, what type of Python object is the number `2157` ?D. How could we make it be a string and insert it into the URL string?<jupyter_code>import json

jsonString = '''{
    "results": [
        2157
        ], 
    "limitedToMaxAllowed": "False"
}'''

dataStructure = json.loads(jsonString)
print(dataStructure)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18152325042484166, 'fraction_numerical': 0.024409083886914876, 'mean_word_length': 4.837691614066727, 'pattern_counts': {'":': 27, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '231718', 'license': 'CC0-1.0', 'n_tokens_mistral': 2274, 'n_tokens_neox': 2068, 'n_words': 580, 'path': 'code/pylesson/json.ipynb', 'repo_name': 'HeardLibrary/digital-scholarship', 'size': '6622'}