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 896811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:47:21+00:00 2026-05-15T14:47:21+00:00

Using the json module in python 2.6, I’m experiencing some unexpected behavior when passing

  • 0

Using the json module in python 2.6, I’m experiencing some unexpected behavior when passing a function to object_hook. I’m attempting to turn a json object into a class defined in my code. This class requires that instances of a different class as arguments. Something like this:

class OuterClass:
    def __init__(self, arg, *InnerClass_instances):
        self.arg = arg
        self.class2 = InnerClass_instances

class InnerClass:
    def __init__(self, name, value):
        self.name = name
        self.value = value 

It seems to me, that if to create a JSON object that contains all of the information for an instance of OuterClass, it would be necessary to include all of the information for each instance of InnerClass that would be passed to it. So, I thought I could write two functions to parse JSON object, one for each class, and have the function creating OuterClass, call the function that creates InnerClass instances. Something like this:

def create_InnerClass(dct):
    if '__InnerClass__' in dct:
        name = dct['name']
        value = dct['value']
        return InnerClass(name, value)

def create_OuterClass(dct):
    if '__OuterClass__' in dct:
        arg = dct['arg']
        InnerClass_instances = [create_InnerClass(dct2) \
                                for dct2 in dct['InnerClass_instances']]
        return OuterClass(arg, *InnerClass_intances)

Which, I assumed would work for

s =            #triple quotes omitted for syntax highlighting reasons
{
"__OuterClass__": true,
"arg": 4,
"InnerClass_instances":
    [
    {"__InnerClass__": true, "name": "Joe", "value": 12},
    {"__InnerClass__": true, "name": "Jimmy", "value":7}
    ]
} 

outerClass = json.loads(s, object_hook = creat_OuterClass)

However, calling json.loads as above returns this error:

if '__InnerClass__' in dct:
TypeError: argument of type 'NoneType' is not iterable

However, simply calling json.loads(s), and then json.dumps(outerClass) works perfectly fine. So, it seems like something in this way of approaching the object_hook parameter is causing the dictionaries that define InnerClass instances to be turned into NoneType objects. Am I missing some understanding as to how the function object_hook is used in parsing JSON objects?

Is there a way to do class hinting for multiple types of classes in a single JSON object? Nothing I’ve read so far has suggested that this is or is not possible.

  • 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-15T14:47:22+00:00Added an answer on May 15, 2026 at 2:47 pm

    Not every time create_OuterClass is called will __OuterClass__ be a key in dct. Therefore, your create_OuterClass should handle this case as well. The default is to return dct:

    def create_OuterClass(dct):
        # print('create_OuterClass: {0}'.format(dct))     
        if '__OuterClass__' in dct:
            arg = dct['arg']
            InnerClass_instances = [create_InnerClass(dct2)
                                        for dct2 in dct['InnerClass_instances']]
            return OuterClass(arg, *InnerClass_instances)
        return dct
    

    If you include the above print statement you get

    create_OuterClass: {u'__InnerClass__': True, u'name': u'Joe', u'value': 12}
    create_OuterClass: {u'__InnerClass__': True, u'name': u'Jimmy', u'value': 7}
    create_OuterClass: {u'__OuterClass__': True, u'InnerClass_instances': [{u'__InnerClass__': True, u'name': u'Joe', u'value': 12}, {u'__InnerClass__': True, u'name': u'Jimmy', u'value': 7}], u'arg': 4}
    

    This shows you how json.loads is using the object_hook callback function.
    It calls it first with the inner dicts, and only later with the outer dict.

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

Sidebar

Related Questions

I'm a Python programmer with experience using the json module. I've just meet CouchDB
I am using the standard json module in python 2.6 to serialize a list
I'm trying to parse some JSON object strings that I'm getting using gson-1.6.jar I
I've been using the json module in python 2.6 but is very slow. I'd
How should I parse JSON using Node.js? Is there some module which will validate
I'm using the json module in Python 2.6 to load and decode JSON files.
Using Pythons (2.7) 'json' module I'm looking to process various JSON feeds. Unfortunately some
I'm using the python module requests to get data from some API's and they
I am using Json.NET to serialize an object graph. For each object that is
I've seen people using *.cfg (Python Buildout), *.xml (Gnome), *.json (Chrome extension), *.yaml (Google

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.