Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1074837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:11:23+00:00 2026-05-16T21:11:23+00:00

I’m trying to load JSON back into an object. The loads method seems to

  • 0

I’m trying to load JSON back into an object. The “loads” method seems to work without error, but the object doesn’t seem to have the properties I expect.

How can I go about examining/inspecting the object that I have (this is web-based code).

  results = {"Subscriber": {"firstname": "Neal", "lastname": "Walters"}}
  subscriber = json.loads(results)


  for item in inspect.getmembers(subscriber): 
     self.response.out.write("<BR>Item")
     for subitem in item: 
         self.response.out.write("<BR>&nbsp;SubItem=" + subitem)

The attempt above returned this:

   Item
     SubItem=__class__

I don’t think it matters, but for context:
The JSON is actually coming from a urlfetch in Google App Engine to
a rest web service created using this utility:
http://code.google.com/p/appengine-rest-server.
The data is being retrieved from a datastore with this definition:

class Subscriber(db.Model):
    firstname    = db.StringProperty()
    lastname     = db.StringProperty()

Thanks,
Neal

Update #1: Basically I’m trying to deserialize JSON back into an object.
In theory it was serialized from an object, and I want to now get it back into an object.
Maybe the better question is how to do that?

Update #2: I was trying to abstract a complex program down to a few lines of code, so I made a few mistakes in “pseudo-coding” it for purposes of posting here.

Here’s a better code sample, now take out of website where I can run on PC.

results = '{"Subscriber": {"firstname": "Neal", "lastname": "Walters"}}'
subscriber = json.loads(results)
for key, value in subscriber.items():
    print " %s: %s" %(key, value)

The above runs, what it displays doesn’t look any more structured than the JSON string itself. It displays this:
Subscriber: {u’lastname’: u’Walters’, u’firstname’: u’Neal’}

I have more of a Microsoft background, so when I hear serialize/deserialize, I think going from an object to a string, and from a string back to an object. So if I serialize to JSON, and then deserialize, what do I get, a dictionary, a list, or an object? Actually, I’m getting the JSON from a REST webmethod, that is on my behalf serializing my object for me.

Ideally I want a subscriber object that matches my Subscriber class above, and ideally, I don’t want to write one-off custom code (i.e. code that would be specific to “Subscriber”), because I would like to do the same thing with dozens of other classes. If I have to write some custom code, I will need to do it generically so it will work with any class.

Update #3: This is to explain more of why I think this is a needed tool. I’m writing a huge app, probably on Google App Engine (GAE). We are leaning toward a REST architecture for several reasons, but one is that our web GUI should access the data store via a REST web layer. (I’m a lot more used to SOAP, so switching to REST is a small challenge in itself). So one of the classic ways of getting and update data is through a business or data tier. By using the REST utility mention above, I have the choice of XML or JSON. I’m hoping to do a small working prototype of both before we develop the huge app). Then, suppose we have a successful app, and GAE doubles it prices. Then we can rewrite just the data tier, and take our Python/Django user tier (web code), and run it on Amazon or somewhere else.

If I’m going to do all that, why would I want everything to be dictionary objects. Wouldn’t I want the power of full-blown class structure? One of the next tricks is sort of an object relational mapping (ORM) so that we don’t necessarily expose our exact data tables, but more of a logical layer.

We also want to expose a RESTful API to paying users, who might be using any language. For them, they can use XML or JSON, and they wouldn’t use the serialize routine discussed here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:11:24+00:00Added an answer on May 16, 2026 at 9:11 pm

    json only encodes strings, floats, integers, javascript objects (python dicts) and lists.

    You have to create a function to turn the returned dictionary into a class and then pass it to a json.loads using the object_hook keyword argument along with the json string. Heres some code that fleshes it out:

    import json
    
    class Subscriber(object):
        firstname = None
        lastname = None
    
    
    class Post(object):
        author = None
        title = None
    
    
    def decode_from_dict(cls,vals):
        obj = cls()
        for key, val in vals.items():
            setattr(obj, key, val)
        return obj
    
    
    SERIALIZABLE_CLASSES = {'Subscriber': Subscriber,
                            'Post': Post}
    
    def decode_object(d):
        for field in d:
            if field in SERIALIZABLE_CLASSES:
                cls = SERIALIZABLE_CLASSES[field]
                return decode_from_dict(cls, d[field])
        return d
    
    
    results = '''[{"Subscriber": {"firstname": "Neal", "lastname": "Walters"}},
                  {"Post": {"author": {"Subscriber": {"firstname": "Neal",
                                                      "lastname": "Walters"}}},
                            "title": "Decoding JSON Objects"}]'''
    result = json.loads(results, object_hook=decode_object)
    print result
    print result[1].author
    

    This will handle any class that can be instantiated without arguments to the constructor and for which setattr will work.

    Also, this uses json. I have no experience with simplejson so YMMV but I hear that they are identical.

    Note that although the values for the two subscriber objects are identical, the resulting objects are not. This could be fixed by memoizing the decode_from_dict class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.