parsing json using simplejson

Discussion in 'Python' started by deostroll, Jun 15, 2009.

  1. deostroll

    deostroll Guest

    I need to be able to parse a json data object using the simplejson
    package. First of all I need to know all the task needed for this job.

    --deostroll
     
    deostroll, Jun 15, 2009
    #1
    1. Advertising

  2. deostroll wrote:

    > I need to be able to parse a json data object using the simplejson
    > package. First of all I need to know all the task needed for this job.


    - install simplejson
    - read documentation of simplejson
    - use simplejson as documented
    - ???
    - profit!

    Diez
     
    Diez B. Roggisch, Jun 15, 2009
    #2
    1. Advertising

  3. On Sun, 14 Jun 2009 22:45:38 -0700 (PDT)
    deostroll <> wrote:

    > I need to be able to parse a json data object using the simplejson
    > package. First of all I need to know all the task needed for this job.


    Note that py2.6 has a bundled json module.

    --
    Mike Kazantsev // fraggod.net

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.11 (GNU/Linux)

    iEYEARECAAYFAko2cOQACgkQASbOZpzyXnHSJgCgv6/POJu1Lw3Nx8mbwyQxBKVW
    lwMAnRfWUZ22bf2zx4ZixeGTbpTcjJr7
    =RqCP
    -----END PGP SIGNATURE-----
     
    Mike Kazantsev, Jun 15, 2009
    #3
  4. deostroll

    deostroll Guest

    I want to be able to parse it into python objects. Any ideas?

    --deostroll
     
    deostroll, Jun 16, 2009
    #4
  5. deostroll

    Carl Banks Guest

    On Jun 15, 8:01 pm, deostroll <> wrote:
    > I want to be able to parse it into python objects. Any ideas?


    1. If applicable, pay better attention in class.
    2. Install simplejson and try to use it, then, if you still need help,
    come back and post your question along with your honest attempt to do
    it.

    P.S. Someone will be by to post the "How to ask smart questions" essay
    shortly.


    Carl Banks
     
    Carl Banks, Jun 16, 2009
    #5
  6. On Mon, 15 Jun 2009 20:01:58 -0700 (PDT)
    deostroll <> wrote:

    > I want to be able to parse it into python objects. Any ideas?


    JSON objects behave like python dicts (key:val pairs), so why not just
    use them?

    Both simplejson and py2.6-json (which is quite similar to the former)
    do just that, but if you like JS attribute-like key access model you
    can use it by extending the builtin dict class:


    import types, collections


    class AttrDict(dict):
    '''AttrDict - dict with JS-like key=attr access'''
    def __init__(self, *argz, **kwz):
    if len(argz) == 1 and not kwz and isinstance(argz[0], types.StringTypes):
    super(AttrDict, self).__init__(open(argz[0]))
    else:
    super(AttrDict, self).__init__(*argz, **kwz)
    for k,v in self.iteritems(): setattr(self, k, v) # re-construct all values via factory

    def __val_factory(self, val):
    return AttrDict(val) if isinstance(val, collections.Mapping) else val

    def __getattr__(self, k):
    return super(AttrDict, self).__getitem__(k)
    __getitem__ = __getattr__

    def __setattr__(self, k, v):
    return super(AttrDict, self).__setitem__(k, self.__val_factory(v))
    __setitem__ = __setattr__


    if __name__ == '__main__':
    import json

    data = AttrDict(json.loads('{"host": "docs.python.org",'
    ' "port": 80,'
    ' "references": [ "collections",'
    ' "json",'
    ' "types",'
    ' "data model" ],'
    ' "see_also": { "UserDict": "similar, although'
    ' less flexible dict implementation." } }'))

    print data.references

    # You can always use it as a regular dict
    print 'port' in data
    print data['see_also']

    # Data model propagnates itself to any sub-mappings
    data.see_also.new_item = dict(x=1, y=2)
    print data.see_also.keys()
    data.see_also.new_item['z'] = 3
    print data.see_also.new_item.z


    --
    Mike Kazantsev // fraggod.net

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.11 (GNU/Linux)

    iEYEARECAAYFAko3IBEACgkQASbOZpzyXnGhkACfQh87il4F0gAI2Lls5n2vo1nV
    ObkAnRfhxzYZIvSJ+hBJAQIqAzOqkRht
    =7Gb9
    -----END PGP SIGNATURE-----
     
    Mike Kazantsev, Jun 16, 2009
    #6
    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. Thomas Heller

    json vs. simplejson

    Thomas Heller, May 13, 2009, in forum: Python
    Replies:
    4
    Views:
    2,395
    Thomas Heller
    May 13, 2009
  2. Aaron
    Replies:
    2
    Views:
    1,663
    Chris Rebert
    Mar 15, 2011
  3. Aaron
    Replies:
    1
    Views:
    330
  4. sajuptpm
    Replies:
    2
    Views:
    350
    sajuptpm
    Dec 28, 2012
  5. David Karr
    Replies:
    1
    Views:
    172
    David Karr
    Jun 17, 2013
Loading...

Share This Page