Constructing JSON data structures from non-string key python dictionaries

Grant Edwards invalid at invalid.invalid
Wed Nov 21 17:04:35 CET 2012

On 2012-11-21, MRAB <python at> wrote:

>> However, I don't know how to do that because dictionary keys in
>> python need to be strings. If I try to do the following, Python,of
>> course, complains that y,color,drilldown, etc are not defined.
> Just quote them:
> data = [ { 'y':55.11, 'color':colors[0], 'drilldown':{'name': 'MSIE 
> versions','categories': ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 
> 9.0'],'data': [10.85, 7.35, 33.06, 2.81],'color': colors[0] }} ]
> Incidentally, dictionary keys in Python don't have to be strings, but
> merely 'hashable', which includes integers, floats and tuples amongst
> others.

I think he meant that in his use case, the Python dictionary keys must
be strings, since that's what JSON requires.

Grant Edwards               grant.b.edwards        Yow! I have a TINY BOWL in
                                  at               my HEAD

More information about the Python-list mailing list