Constructing JSON data structures from non-string key python dictionaries

saikari78 saikari78 at
Wed Nov 21 15:59:11 CET 2012


I'm using the json module to  create a JSON string, then inserting that string into a html template containing a javascript function (from the highcharts library:
The json string I'm trying to create is to initialize a data variable in the javascript function, that has the following example format.

            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]

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.

import json

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] }} ]       

data_string = json.dumps(data)

Many thanks for any suggestions on how to do this.

More information about the Python-list mailing list