JSON tutorial question

This is the place for queries that don't fit in any of the other categories.

JSON tutorial question

Postby David05 » Thu Nov 21, 2013 10:50 am

Hey guys,

I'm an experienced programmer but I guess I gotta move with the times and learn one of these newer languages. How do I complete this exercise?

http://www.learnpython.org/Serializatio ... d%20pickle
Last edited by Mekire on Thu Nov 21, 2013 11:37 am, edited 1 time in total.
Reason: First post lock.
David05
 
Posts: 3
Joined: Thu Nov 21, 2013 10:44 am

Re: JSON tutorial question

Postby Mekire » Thu Nov 21, 2013 11:56 am

You should detail some of the things you have tried so far. I got the test to pass by loading the json into a variable; adding the value to the dict; then redumping as json. This required adding an import line to the top of the code.

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: JSON tutorial question

Postby David05 » Tue Nov 26, 2013 8:10 am

The link seems to have gotten corrupted so here it is again:

http://www.learnpython.org/Serialization

And sorry for the delay in replying; I thought I was going to get an email when my post was officially approved. :?

Well this is what I did but I'm getting a run time error. I'm trying to manipulate the json string to append the new salary pair. Am I on the right track?

Code: Select all
import json

# fix this function, so it adds the given name
# and salary pair to salaries_json, and return it
def add_employee(salaries_json, name, salary):
    # Add your code here
    salaries_json  = salaries_json[:-1] + '"' + name + '" : ' + str(salary) + '}'
    print salaries_json
    return salaries_json

# test code
salaries = '{"Alfred" : 300, "Jane" : 400 }'
new_salaries = add_employee(salaries, "Me", 800)
decoded_salaries = json.loads(new_salaries)
print decoded_salaries["Alfred"]
print decoded_salaries["Jane"]
print decoded_salaries["Me"]


I really have no idea how to do this question that's why I posted here.
David05
 
Posts: 3
Joined: Thu Nov 21, 2013 10:44 am

Re: JSON tutorial question

Postby metulburr » Tue Nov 26, 2013 8:22 am

next time display the exact error you get along with code that gives the error.

i would think that this is self-explanatory:
Code: Select all
Traceback (most recent call last):
  File "test.py", line 17, in <module>
    decoded_salaries = json.loads(new_salaries)
  File "/usr/lib/python3.3/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.3/json/decoder.py", line 352, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.3/json/decoder.py", line 368, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting ',' delimiter: line 1 column 31 (char 30)


You forgot to add the comma separator between the previous entry and the new entry.
printing should give you this before the error:
Code: Select all
{"Alfred" : 300, "Jane" : 400 "Me" : 800}

note the missing comman before 400 and "Me".

modify the line to this should add that in:
Code: Select all
salaries_json  = salaries_json[:-1] + ", " + '"' + name + '" : ' + str(salary) + '}'
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1416
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: JSON tutorial question

Postby David05 » Tue Nov 26, 2013 8:52 am

Thanks! :)
David05
 
Posts: 3
Joined: Thu Nov 21, 2013 10:44 am

Re: JSON tutorial question

Postby Mekire » Tue Nov 26, 2013 9:24 am

Editing the string manually is pretty silly.
Load the actual object from the json.
Insert the new key:value pair.
Save the new object in a json string.
return it.

Code: Select all
def add_employee(salaries_json, name, salary):
    # Add your code here
    data = json.loads(salaries_json)
    data[name] = salary
    salaries_json = json.dumps(data)
    return salaries_json

The link in the original post seems to be broken now; but when I tested this previously it worked.

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests