Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33739 > unrolled thread
| Started by | saikari78 <saikari78@gmail.com> |
|---|---|
| First post | 2012-11-21 06:59 -0800 |
| Last post | 2012-11-21 08:04 -0800 |
| Articles | 11 — 6 participants |
Back to article view | Back to comp.lang.python
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
| From | saikari78 <saikari78@gmail.com> |
|---|---|
| Date | 2012-11-21 06:59 -0800 |
| Subject | Constructing JSON data structures from non-string key python dictionaries |
| Message-ID | <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-11-21 15:48 +0000 |
| Message-ID | <mailman.157.1353512886.29569.python-list@python.org> |
| In reply to | #33739 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2012-11-21 16:04 +0000 |
| Message-ID | <k8iu2j$va$1@reader1.panix.com> |
| In reply to | #33740 |
On 2012-11-21, MRAB <python@mrabarnett.plus.com> wrote:
>> 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.
>
> 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.
I think he meant that in his use case, the Python dictionary keys must
be strings, since that's what JSON requires.
--
Grant Edwards grant.b.edwards Yow! I have a TINY BOWL in
at my HEAD
gmail.com
[toc] | [prev] | [next] | [standalone]
| From | hfolch@gmail.com |
|---|---|
| Date | 2012-11-21 08:04 -0800 |
| Message-ID | <448f6f44-2387-4a2d-9131-084b3585443e@googlegroups.com> |
| In reply to | #33740 |
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.
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-11-21 16:27 +0000 |
| Message-ID | <mailman.160.1353515271.29569.python-list@python.org> |
| In reply to | #33742 |
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.
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-11-21 16:43 +0000 |
| Message-ID | <mailman.161.1353516195.29569.python-list@python.org> |
| In reply to | #33742 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2012-11-21 11:04 -0800 |
| Message-ID | <mailman.167.1353524703.29569.python-list@python.org> |
| In reply to | #33742 |
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
[toc] | [prev] | [next] | [standalone]
| From | saikari78 <saikari78@gmail.com> |
|---|---|
| Date | 2012-11-22 09:05 -0800 |
| Message-ID | <33d001d3-c1d6-4bb3-9448-8781c396f561@googlegroups.com> |
| In reply to | #33757 |
Thanks for all these very clarifying and useful replies
[toc] | [prev] | [next] | [standalone]
| From | saikari78 <saikari78@gmail.com> |
|---|---|
| Date | 2012-11-22 09:05 -0800 |
| Message-ID | <mailman.209.1353603958.29569.python-list@python.org> |
| In reply to | #33757 |
Thanks for all these very clarifying and useful replies
[toc] | [prev] | [next] | [standalone]
| From | Paul Kölle <paul@subsignal.org> |
|---|---|
| Date | 2012-11-22 09:53 +0100 |
| Message-ID | <mailman.197.1353574471.29569.python-list@python.org> |
| In reply to | #33742 |
Am 21.11.2012 17:04, schrieb hfolch@gmail.com: > Thanks for your reply, but the javascript function expects option > names to be unquoted, otherwise it won't work. Others have shown you how to solve this, but I would like to note that the function does NOT expect JSON but a simple javascript object literal. cheers Paul
[toc] | [prev] | [next] | [standalone]
| From | hfolch@gmail.com |
|---|---|
| Date | 2012-11-21 08:04 -0800 |
| Message-ID | <mailman.158.1353513898.29569.python-list@python.org> |
| In reply to | #33740 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web