Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33746

Re: Constructing JSON data structures from non-string key python dictionaries

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
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; 'mrab': 0.05; '21,': 0.07; '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; '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; '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; 'dictionary': 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; '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; '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=foTs_y1HMH3b9uGL7pIA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Wed, 21 Nov 2012 16:27:50 +0000
From MRAB <python@mrabarnett.plus.com>
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> <mailman.157.1353512886.29569.python-list@python.org> <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com>
In-Reply-To <448f6f44-2387-4a2d-9131-084b3585443e@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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.160.1353515271.29569.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1353515271 news.xs4all.nl 6975 [2001:888:2000:d::a6]:51568
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33746

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Constructing JSON data structures from non-string key python dictionaries saikari78 <saikari78@gmail.com> - 2012-11-21 06:59 -0800
  Re: Constructing JSON data structures from non-string key python dictionaries MRAB <python@mrabarnett.plus.com> - 2012-11-21 15:48 +0000
    Re: Constructing JSON data structures from non-string key python dictionaries Grant Edwards <invalid@invalid.invalid> - 2012-11-21 16:04 +0000
    Re: Constructing JSON data structures from non-string key python dictionaries hfolch@gmail.com - 2012-11-21 08:04 -0800
      Re: Constructing JSON data structures from non-string key python dictionaries MRAB <python@mrabarnett.plus.com> - 2012-11-21 16:27 +0000
      Re: Constructing JSON data structures from non-string key python dictionaries MRAB <python@mrabarnett.plus.com> - 2012-11-21 16:43 +0000
      Re: Constructing JSON data structures from non-string key python dictionaries Chris Rebert <clp2@rebertia.com> - 2012-11-21 11:04 -0800
        Re: Constructing JSON data structures from non-string key python dictionaries saikari78 <saikari78@gmail.com> - 2012-11-22 09:05 -0800
        Re: Constructing JSON data structures from non-string key python dictionaries saikari78 <saikari78@gmail.com> - 2012-11-22 09:05 -0800
      Re: Constructing JSON data structures from non-string key python dictionaries Paul Kölle <paul@subsignal.org> - 2012-11-22 09:53 +0100
    Re: Constructing JSON data structures from non-string key python dictionaries hfolch@gmail.com - 2012-11-21 08:04 -0800

csiph-web