parsing json using simplejson

D

deostroll

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
 
D

Diez B. Roggisch

deostroll said:
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
 
M

Mike Kazantsev

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-----
 
C

Carl Banks

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
 
M

Mike Kazantsev

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-----
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top