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


Groups > comp.lang.python > #33743

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

Return-Path <hfolch@gmail.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; 'to:addr:comp.lang.python': 0.09; 'cc:addr :python-list': 0.10; 'template': 0.11; 'subject:python': 0.11; 'url:)': 0.13; 'complains': 0.16; 'expects': 0.16; 'inserting': 0.16; 'integers,': 0.16; 'merely': 0.16; 'subject:dictionaries': 0.16; 'subject:key': 0.16; 'subject:non': 0.16; 'utc,': 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; 'wednesday,': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'work.': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'dictionary': 0.29; 'strings,': 0.29; 'this.': 0.29; "i'm": 0.29; "skip:' 10": 0.30; '(from': 0.30; 'function': 0.30; "skip:' 20": 0.32; 'subject:data': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'received:209.85.220': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'but': 0.36; 'others.': 0.36; 'option': 0.37; 'skip:v 20': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'subject:-': 0.40; 'your': 0.60; 'from:no real name:2**0': 0.60; 'containing': 0.61; 'amongst': 0.91; 'received:209.85.220.184': 0.91; 'reply,': 0.93
Newsgroups comp.lang.python
Date Wed, 21 Nov 2012 08:04:49 -0800 (PST)
In-Reply-To <mailman.157.1353512886.29569.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=193.1.186.252; posting-account=EKIpZgoAAADGiJ9JDpJss7yvQg8LGL3H
References <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> <mailman.157.1353512886.29569.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 193.1.186.252
MIME-Version 1.0
Subject Re: Constructing JSON data structures from non-string key python dictionaries
From hfolch@gmail.com
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
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>
Message-ID <mailman.158.1353513898.29569.python-list@python.org> (permalink)
Lines 85
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1353513898 news.xs4all.nl 6870 [2001:888:2000:d::a6]:37295
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:33743

Show key headers only | View raw


Thanks for your reply, but the javascript function expects option names to be unquoted, otherwise it won't work.
 


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.

Back to comp.lang.python | Previous | NextPrevious 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