Thread: Json

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    27
    Rep Power
    0

    Json


    I'm starting to read up on json and work with it, but I have a quick question.

    So something less complicated - if I were given a dict json_str like so:
    Code:
    json_str = '{"Jose": 6, "May": 3, "Haha": 2, "Jones": 1, "Lucy": 1}'
    and need to return the sum of the values in the dict to return, for this ex. 13 (since 6+3+2+1+1=13)

    How would I go about doing something like that?

    Would I be using json.loads or json.dumps?
    I've tried both and json.dumps gives me a weird ouput with \\
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Code:
    >>> json.loads(json_str)
    {u'May': 3, u'Jose': 6, u'Haha': 2, u'Jones': 1, u'Lucy': 1}
    >>> sum(json.loads(json_str).values())
    13
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    27
    Rep Power
    0
    Thank you b49P23TIvg for all your help so far! I really really appreciate it!

IMN logo majestic logo threadwatch logo seochat tools logo