Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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; 'else:': 0.03; 'elif': 0.04; 'initialize': 0.05; 'mrab': 0.05; '21,': 0.07; 'data:': 0.07; 'function,': 0.07; 'json': 0.07; 'strings.': 0.07; 'python': 0.09; '"["': 0.09; 'defined.': 0.09; 'function:': 0.09; 'subject:string': 0.09; 'def': 0.10; 'template': 0.11; 'subject:python': 0.11; 'url:)': 0.13; '"]"': 0.16; '"{"': 0.16; 'complains': 0.16; 'expects': 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; 'list):': 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; 'utc,': 0.16; 'string': 0.17; 'wrote:': 0.17; "shouldn't": 0.17; 'string,': 0.17; '>>>': 0.18; 'module': 0.19; 'variable': 0.20; 'trying': 0.21; 'import': 0.21; 'keys': 0.22; 'tuples': 0.22; 'wednesday,': 0.22; 'example': 0.23; 'work.': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; '>>>>': 0.29; 'dictionary': 0.29; 'key,': 0.29; 'received:192.168.1.3': 0.29; 'strings,': 0.29; 'types.': 0.29; 'source': 0.29; 'this.': 0.29; "i'm": 0.29; "skip:' 10": 0.30; '(from': 0.30; 'function': 0.30; 'code': 0.31; 'received:84': 0.32; "skip:' 20": 0.32; 'subject:data': 0.33; 'to:addr:python- list': 0.33; 'hi,': 0.33; 'skip:d 20': 0.34; 'thanks': 0.34; "won't": 0.35; 'but': 0.36; 'others.': 0.36; 'too': 0.36; 'option': 0.37; 'skip:v 20': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'subject:-': 0.40; 'your': 0.60; 'range': 0.60; 'containing': 0.61; 'email addr:gmail.com': 0.63; '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; 'reply,': 0.93 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=pGLkceISAAAA:8 a=--wz9vYVAAAA:8 a=M4LjhnQNyFSt_jFXmBYA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Wed, 21 Nov 2012 16:43:15 +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> <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> <50AD0106.6020303@mrabarnett.plus.com> In-Reply-To: <50AD0106.6020303@mrabarnett.plus.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: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1353516195 news.xs4all.nl 6842 [2001:888:2000:d::a6]:60488 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33748 On 2012-11-21 16:27, MRAB wrote:> On 2012-11-21 16:04, hfolch@gmail.com wrote: >> On Wednesday, November 21, 2012 3:48:07 PM UTC, MRAB wrote: >>> 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. >> >> Thanks for your reply, but the javascript function expects option >> names to be unquoted, otherwise it won't work. > > Both Python source code and JSON require the dictionary keys to be > quoted, so using the json module to generate JavaScript code isn't > going to give you what you want. > > It shouldn't be too difficult to write a simple function to do it, > considering the limited range of types. > Here's such a function: def dump_javascript(data): if isinstance(data, dict): items = [] for key, value in data.items(): items.append(key + ": " + dump_javascript(value)) return "{" + ", ".join(items) + "}" elif isinstance(data, list): items = [] for value in data: items.append(dump_javascript(value)) return "[" + ", ".join(items) + "]" else: return json.dumps(data)