Stuck on learnpython.org exercise using JSON
Question: <http://www.learnpython.org/Serialization_using_JSON_and_pickle>

Here are the instructions:

The aim of this exercise is to print out the JSON string with key-value pair
"Me" : 800 added to it.

And below is the starting code, which we should modify.

    
    
    #Exercise fix this function, so it adds the given name and salary pair to the json it returns
    def add_employee(jsonSalaries, name, salary): 
        # Add your code here
    
        return jsonSalaries 
    
    #Test code - shouldn't need to be modified
    originalJsonSalaries = '{"Alfred" : 300, "Jane" : 301 }'
    newJsonSalaries = add_employee(originalJsonSalaries, "Me", 800)
    print(newJsonSalaries)
    

I'm completely lost. The JSON lesson was brief, at best. The issue I seem to
be running in to here is that `orginalJsonSalaries` is defined as a string
(containing all sort of unnecessary symbols like brackets. In fact, I think if
the single quotes surrounding its definition were removed,
`originalJsonSalaries` would be a dictionary and this would be a lot easier.
But as it stands, how can I append `"Me"` and `800` to the string and still
maintain the dictionary-like formatting?

And yes, I'm very very new to coding. The only other language I know is tcl.

EDIT:

OK, thanks to the answers, I figured out I was being dense and I wrote this
code:

    
    
    import json
    #Exercise fix this function, so it adds the given name and salary pair to the json it returns
    def add_employee(jsonSalaries, name, salary):
        # Add your code here
        jsonSalaries = json.loads(jsonSalaries)
        jsonSalaries["Me"] = 800
        return jsonSalaries 
    
    #Test code - shouldn't need to be modified
    originalJsonSalaries = '{"Alfred" : 300, "Jane" : 301 }'
    newJsonSalaries = add_employee(originalJsonSalaries, "Me", 800)
    print(newJsonSalaries)
    

This does not work. For whatever reason, the original dictionary keys are
formatted as unicode (I don't know where that happened), so when I print out
the dictionary, the "u" flag is shown:

    
    
    {u'Jane': 301, 'Me': 800, u'Alfred': 300}
    

I have tried using `dict.pop()` to replace the key ( `dict("Jane") =
dict.pop(u"Jane")` ) but that just brings up `SyntaxError: can't assign to
function call`

Is my original solution incorrect, or is this some annoying formatting issue
and how to resolve it?


Answer: The [page you linked
to](http://www.learnpython.org/Serialization_using_JSON_and_pickle) says
exactly how to do this:

> In order to use the json module, it must first be imported:
>  
>  
>     import json
>  
>
> [...]
>
> To load JSON back to a data structure, use the "loads" method. This method
> takes a string and turns it back into the json object datastructure:
>  
>  
>     print json.loads(json_string)
>  

They gave you a string (`jsonSalaries`). Use `json.loads` to turn it into a
dictionary.

