python - json.dump() gives me "TypeError: keys must be a string" -


i have simple code takes dictionary tuple key , turns json:

in [11]:  import simplejson json in [12]:  data = {('category1', 'category2'): 4} in [13]:  json.dumps(data) 

however, running code gives me:

typeerror: keys must string 

i have tried str()'ing keys , else can find, without luck.

the error seems pretty clear: keys must strings.

if take example question , str()-ify keys:

>>> data = {str(('category1', 'category2')): 4} 

it works fine:

>>> json.dumps(data) '{"(\'category1\', \'category2\')": 4}' 

having said that, in position consider making keys more readable. possibly like:

>>> data = dict((':'.join(k), v) k,v in data.items()) 

this turns key ('category1', 'category2') category1:category2,


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -