Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'unicode' object has no attribute 'values' when parsing JSON dictionary values

I have the following JSON dictionary:

{
 u'period': 16, u'formationName': u'442', u'formationId': 2, 
 u'formationSlots': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0, 0], 
 u'jerseyNumbers': [1, 20, 3, 15, 17, 5, 19, 6, 18, 25, 10, 2, 4, 12, 16, 22, 24, 
                    34], 
 u'playerIds': [23122, 38772, 24148, 39935, 29798, 75177, 3860, 8505, 
               26013, 3807, 34693, 18181, 4145, 23446, 8327, 107395, 29762, 254558], 
 u'captainPlayerId': 29798, 
 u'startMinuteExpanded': 0, 
 u'endMinuteExpanded': 82, 
 u'formationPositions': [{u'horizontal': 5.0, u'vertical': 0.0}, 
     {u'horizontal': 1.0, u'vertical': 2.5}, {u'horizontal': 9.0, u'vertical': 2.5}, 
     {u'horizontal':3.5, u'vertical': 6.0}, {u'horizontal': 3.5, u'vertical': 2.5}, 
     {u'horizontal': 6.5, u'vertical': 2.5}, {u'horizontal': 1.0, u'vertical': 6.0}, 
     {u'horizontal': 6.5, u'vertical': 6.0}, {u'horizontal': 6.5, u'vertical': 9.0}, 
     {u'horizontal': 3.5, u'vertical': 9.0}, {u'horizontal': 9.0, u'vertical': 6.0}]
}

As you can see some of the dictionary values are contained within lists. I am trying to obtain all the values from this object programatically like so:

for myvalue in myjsonobject:
    print mydict
    for mysubvalue in myvalue:
        print mysubvalue

This prints the dictionary keys:

period
formationName
formationId
formationSlots
jerseyNumbers
playerIds
captainPlayerId
startMinuteExpanded
endMinuteExpanded
formationPositions

When what I actually want is the values. I have tried replacing the line print mysubvalue with print mysubvalue.values(), however this causes the following error:

Traceback (most recent call last):
  File "C:\Python27\counter.py", line 78, in <module>
    print mysubdict.values()
AttributeError: 'unicode' object has no attribute 'values'

I'm taking an educated guess here that I dont need to use json.loads(mysubdict) to allow me access the .values() function. If so, I am not sure why I am getting this error.

Can anyone assist?

Thanks

like image 771
gdogg371 Avatar asked Jan 16 '15 19:01

gdogg371


2 Answers

If you iterate over the dictionary itself (for myvalue in myjsonobject), you will be iterating over the keys of the dictionary. When looping with a for loop, the behavior will be the same whether you loop over the dict (myjsonobject) itself, myjsonobject.keys(), or myjsonobject.iterkeys(). dict.iterkeys() is generally preferable because it is explicit and efficient:

for myvalue in myjsonobject.iterkeys():

like image 62
Ranvijay Sachan Avatar answered Sep 22 '22 02:09

Ranvijay Sachan


You're iterating over the keys of the JSON dictionary, and then calling .values() on each key.

for myvalue in myjsonobject:

iterates over the keys. So when you get to a key that is a string, let's say, u'period' : 16, it will print 'period'.values(), which spits out that error as the string class has no .values().

If you want to flatten the entire JSON dictionary to an arbitrary depth, I would suggest a recursive approach.

like image 1
Andrew Avatar answered Sep 23 '22 02:09

Andrew