Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33757
| Path | csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <chris@rebertia.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; 'python.': 0.02; 'python,': 0.02; '(at': 0.03; 'initialize': 0.05; 'mrab': 0.05; '21,': 0.07; 'data:': 0.07; 'function,': 0.07; 'javascript,': 0.07; 'json': 0.07; 'key.': 0.07; 'strings.': 0.07; 'python': 0.09; 'defined.': 0.09; 'dumps': 0.09; 'jquery.': 0.09; 'received:mail- vb0-f46.google.com': 0.09; 'rfc': 0.09; 'separately': 0.09; 'subject:string': 0.09; 'unmodified': 0.09; 'cc:addr:python-list': 0.10; 'template': 0.11; 'subject:python': 0.11; 'suggest': 0.11; 'url:)': 0.13; 'complains': 0.16; 'expects': 0.16; 'inserting': 0.16; 'integers,': 0.16; 'merely': 0.16; 'plus,': 0.16; 'subject:dictionaries': 0.16; 'subject:key': 0.16; 'subject:non': 0.16; 'templating': 0.16; '});': 0.16; 'wed,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'string,': 0.17; 'received:209.85.212.46': 0.18; 'module': 0.19; 'variable': 0.20; 'equivalent': 0.20; 'skip:= 10': 0.20; 'trying': 0.21; 'import': 0.21; 'assumes': 0.22; 'keys': 0.22; 'latter': 0.22; 'tuples': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'work.': 0.23; 'least': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '(see': 0.27; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'received:209.85.212': 0.28; 'chris': 0.28; 'consult': 0.29; 'dictionary': 0.29; 'quoting': 0.29; 'strings,': 0.29; 'thus,': 0.29; 'this.': 0.29; "i'm": 0.29; "skip:' 10": 0.30; '(from': 0.30; 'function': 0.30; 'expect': 0.31; 'point': 0.31; '(and': 0.32; 'generally': 0.32; "skip:' 20": 0.32; "aren't": 0.33; 'loading': 0.33; 'subject:data': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'nov': 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; 'object': 0.38; 'some': 0.38; 'instead': 0.39; 'subject:-': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'containing': 0.61; 'skip:n 10': 0.63; 'limit': 0.65; 'webpage': 0.65; 'skip:$ 10': 0.66; 'absolutely': 0.84; 'copy-paste': 0.84; 'dict()': 0.84; 'experiencing': 0.84; 'sender:addr:chris': 0.84; 'amongst': 0.91; 'approach.': 0.91; 'reply,': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=UWXq4GZy7klElIYx2OmH1QR1Ber6fliWxMEcOZaatFw=; b=YAi3JN0hM1cWxRge9cd0qLRokVXr2OcSRref2ggzLLY9Gw6GN1gRHPvrSo7tkS0pGJ sEl8YKpGLRczjiY1CFjEcvElYoKDQqzgWC2o2G9ZyvkRDjnKaLyGr/NU3GPQQwwB+/5O qMinKSR5BJwps9Pq55lQlu4nnMHxJWamW8tfU= |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :x-gm-message-state; bh=UWXq4GZy7klElIYx2OmH1QR1Ber6fliWxMEcOZaatFw=; b=SeQjkiHw4xCW+qVTLi2b6jsBjT4HpPU6QZrQDnubaaLHVLkLCJaSylmKqrDS/AWpfS hmJOW4WhZHfpbzRDRNWd1n2QXX5Yu+rveRtcGk7+pSj1mYhkQV3/MBTqsybxi6ySMB8e Gk3fVDWiuP5J4Wis+EgY3Z3BaDcXP+G4tlnXAjDcWA+yKe7/WBNE3GYU1JQyZeMLhwDU FTp8B4bPcy59wO1nyo555CtlmbEelEfU/O8aJM1G5iWsF15yLx57qYXz46FKwmmX9Xd3 30N5pmt/7OD8L6htjyvXUGt6HA5QdASWpXyHtY8Zw+9mMIc15y3jGaF9IDMZOZ+xvSAY ox5Q== |
| MIME-Version | 1.0 |
| Sender | chris@rebertia.com |
| In-Reply-To | <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> |
| References | <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> <mailman.157.1353512886.29569.python-list@python.org> <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> |
| Date | Wed, 21 Nov 2012 11:04:53 -0800 |
| X-Google-Sender-Auth | 5bOeIPGhb4sjQALWc0MNkPTDlFM |
| Subject | Re: Constructing JSON data structures from non-string key python dictionaries |
| From | Chris Rebert <clp2@rebertia.com> |
| To | hfolch@gmail.com |
| Content-Type | text/plain; charset=UTF-8 |
| X-Gm-Message-State | ALoCoQkui3099zXlXDUMThbjYVspPc7THKvLOtWwlBfsTGwbkEfmIcvzKM898AJvLovBXEMdugwp |
| Cc | Python <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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.167.1353524703.29569.python-list@python.org> (permalink) |
| Lines | 106 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1353524703 news.xs4all.nl 6840 [2001:888:2000:d::a6]:39133 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:33757 |
Show key headers only | View raw
On Wed, Nov 21, 2012 at 7:48 AM, MRAB <python@mrabarnett.plus.com> 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/)
Nontrivial templating of JavaScript is generally a bad/inelegant
approach. I would instead suggest generating the JSON separately and
loading it from JavaScript via $.getJSON or similar. Or sticking the
JSON into a hidden part of the webpage and then using JSON.parse().
>> 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.
On Wed, Nov 21, 2012 at 8:04 AM, <hfolch@gmail.com> wrote:
> Thanks for your reply, but the javascript function expects option names to be unquoted, otherwise it won't work.
As a user of HighCharts (and thus, unfortunately, JavaScript), I can
tell you that that's absolutely incorrect.
In JavaScript, {x : y}, {"x" : y}, and {'x' : y} are all equivalent
(at least when x is a valid JavaScript identifier; consult some
non-w3schools JavaScript docs).
Plus, you say you're using JSON; JSON *explicitly mandates that the
keys be quoted* (see RFC 4627).
You are experiencing Python NameErrors because {"x" : y} and {x : y}
aren't equivalent in Python. Python doesn't limit dicts keys to
strings, so `x` is a variable in the latter snippet; x's value is used
as the key.
You cannot expect to take arbitrary, unmodified JavaScript
code/literals, copy-paste them into Python, and expect them to work.
TL;DR:
# foo.py
from json import dumps
colors = SOME_LIST
data = [dict( # use dict() to avoid tedious quoting
y=55.11,
color=colors[0],
drilldown=dict(
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],
)
)]
your_json = dumps(data)
# ...serve the JSON somehow...
================
// bar.js
// Not industrial-strength. Assumes the use of jQuery.
// ...
$.getJSON(SOME_URL, function (data) {
// use 'data', which will be a JavaScript object by this point
});
// ...
Regards,
Chris
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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