Constructing JSON data structures from non-string key python dictionaries

Discussion in 'Python' started by saikari78, Nov 21, 2012.

  1. saikari78

    saikari78 Guest

    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.
    saikari78, Nov 21, 2012
    #1
    1. Advertising

  2. saikari78

    MRAB Guest

    Re: Constructing JSON data structures from non-string key pythondictionaries

    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.
    MRAB, Nov 21, 2012
    #2
    1. Advertising

  3. Re: Constructing JSON data structures from non-string key pythondictionaries

    On 2012-11-21, MRAB <> 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
    Grant Edwards, Nov 21, 2012
    #3
  4. saikari78

    Guest

    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.
    , Nov 21, 2012
    #4
  5. saikari78

    Guest

    Re: Constructing JSON data structures from non-string key pythondictionaries

    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.
    , Nov 21, 2012
    #5
  6. saikari78

    MRAB Guest

    Re: Constructing JSON data structures from non-string key pythondictionaries

    On 2012-11-21 16:04, 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.
    MRAB, Nov 21, 2012
    #6
  7. saikari78

    MRAB Guest

    Re: Constructing JSON data structures from non-string key pythondictionaries

    On 2012-11-21 16:27, MRAB wrote:> On 2012-11-21 16:04,
    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)
    MRAB, Nov 21, 2012
    #7
  8. saikari78

    Chris Rebert Guest

    Re: Constructing JSON data structures from non-string key pythondictionaries

    On Wed, Nov 21, 2012 at 7:48 AM, 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/)


    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, <> 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
    Chris Rebert, Nov 21, 2012
    #8
  9. Re: Constructing JSON data structures from non-string key pythondictionaries

    Am 21.11.2012 17:04, schrieb :
    > 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
    Paul Kölle, Nov 22, 2012
    #9
  10. saikari78

    saikari78 Guest

    Thanks for all these very clarifying and useful replies
    saikari78, Nov 22, 2012
    #10
  11. saikari78

    saikari78 Guest

    Re: Constructing JSON data structures from non-string key pythondictionaries

    Thanks for all these very clarifying and useful replies
    saikari78, Nov 22, 2012
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alfonso Morra
    Replies:
    11
    Views:
    703
    Emmanuel Delahaye
    Sep 24, 2005
  2. sajuptpm
    Replies:
    2
    Views:
    316
    sajuptpm
    Dec 28, 2012
  3. Sean Murphy
    Replies:
    0
    Views:
    79
    Sean Murphy
    Jan 7, 2014
  4. Sean Murphy
    Replies:
    1
    Views:
    72
    Steven D'Aprano
    Jan 7, 2014
  5. Frank Millman
    Replies:
    0
    Views:
    75
    Frank Millman
    Jan 7, 2014
Loading...

Share This Page