Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'initialize': 0.05; 'data:': 0.07; 'function,': 0.07; 'json': 0.07; 'strings.': 0.07; 'python': 0.09; 'defined.': 0.09; 'subject:string': 0.09; 'template': 0.11; 'subject:python': 0.11; 'url:)': 0.13; 'complains': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'inserting': 0.16; 'integers,': 0.16; 'merely': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject: \n ': 0.16; 'subject:dictionaries': 0.16; 'subject:key': 0.16; 'subject:non': 0.16; 'string': 0.17; 'wrote:': 0.17; 'string,': 0.17; 'module': 0.19; 'variable': 0.20; 'trying': 0.21; 'import': 0.21; 'keys': 0.22; 'tuples': 0.22; 'example': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'dictionary': 0.29; 'received:192.168.1.3': 0.29; 'strings,': 0.29; 'this.': 0.29; "i'm": 0.29; "skip:' 10": 0.30; '(from': 0.30; 'function': 0.30; 'received:84': 0.32; "skip:' 20": 0.32; 'subject:data': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'thanks': 0.34; 'but': 0.36; 'others.': 0.36; 'skip:v 20': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'subject:-': 0.40; 'containing': 0.61; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'reply-to:addr:python.org': 0.84; 'amongst': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=Bd1aI8R2 c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=1EKJ2blYo8kA:10 a=6wzhY5gSqFMA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=DW3E9mViqdMA:10 a=--wz9vYVAAAA:8 a=TduWjEGRz5aejk0XGyUA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Wed, 21 Nov 2012 15:48:00 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20121026 Thunderbird/16.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Constructing JSON data structures from non-string key python dictionaries References: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> In-Reply-To: <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1353512886 news.xs4all.nl 6962 [2001:888:2000:d::a6]:34186 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33740 On 2012-11-21 14:59, saikari78 wrote: > Hi, > > 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: http://www.highcharts.com/) > 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. > 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.