Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unhashable type: dict with test JSON

Tags:

python

json

When I try to test some JSON in the python interpreter I get the error. I'm not sure why.

Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
TypeError: unhashable type: 'dict'

JSON: (doesn't work)

b = {
        'data':{
                'child':{
                        {'kid1':'one'},
                        {'kid2':'two'},
                        {'kid3':'three'}
                },
                'child':{
                        {'kid4':'four'},
                        {'kid5':'five'},
                        {'kid6':'six'}
                }
        }
    }

JSON: (works)

a = {
     "slate" : {
         "id" : {
             "type" : "integer"
         },
         "name" : {
             "type" : "string"
         },
         "code" : {
             "type" : "integer",
            "fk" : "banned.id"
         }
     },
     "banned" : {
         "id" : {
             "type" : "integer"
         },
         "domain" : {
             "type" : "string"
         }
     }
 }
like image 359
Liondancer Avatar asked Nov 14 '13 01:11

Liondancer


2 Answers

The reason your first example doesn't work is that each 'child' key has a dictionary declared as it's value instead of a list, as it looks like you intended. Replace the { with [ and it will work.

'child': {
    {'kid1':'one'},
    {'kid2':'two'},
    {'kid3':'three'},
},

Should be:

'child': [
    {'kid1':'one'},
    {'kid2':'two'},
    {'kid3':'three'},
],

In other words, you're saying 'child' is a dictionary without giving a dictionary.

like image 71
senordev Avatar answered Oct 16 '22 16:10

senordev


This issue came up for me when I had slightly malformed JSON:

json = {
    {
        "key": "value",
        "key_two": "value_two"
    }
}

Should be:

json = {
    "key": "value",
    "key_two": "value_two"
}
like image 21
fIwJlxSzApHEZIl Avatar answered Oct 16 '22 17:10

fIwJlxSzApHEZIl