Skip to content

Instantly share code, notes, and snippets.

@bencharb
Created March 29, 2014 18:41
Show Gist options
  • Save bencharb/9860373 to your computer and use it in GitHub Desktop.
Save bencharb/9860373 to your computer and use it in GitHub Desktop.
dict to properties: foo.get('bar') to foo.bar (dict to object with dotted notation properties)
# From Nery Jr on SO: http://stackoverflow.com/a/21502348
# If you want to save lines of code and leave the most flexible solution, we can deserialize the json string to a dynamic object:
p = lambda:None
p.__dict__ = json.loads('{"action": "print", "method": "onData", "data": "Madan Mohan"}')
# >>>> p.action
# output: u'print'
# >>>> p.method
# output: u'onData'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment