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

  • Home
  • SEARCH
  • 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 87699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:26:37+00:00 2026-05-10T22:26:37+00:00

I’m creating a simple API that creates typed classes based on JSON data that

  • 0

I’m creating a simple API that creates typed classes based on JSON data that has a mandatory ‘type’ field defined in it. It uses this string to define a new type, add the fields in the JSON object, instantiate it, and then populate the fields on the instance.

What I want to be able to do is allow for these types to be optionally pre-defined in whatever application is using my module. This is so methods and other application-specific attributes not found in the JSON object can be added. I’d like to have my module check if a type already exists, and if it does, use that instead of dynamically creating the type. It would still add attributes from the JSON object, but it would be recycling the existing type.

My JSON data is:

{     'type': 'Person',     'firstName': 'John',     'lastName': 'Smith',     'address': {         'streetAddress': '21 2nd Street',         'city': 'New York',         'state': 'NY',         'postalCode': 10021     },     'phoneNumbers': [         '212 555-1234',         '646 555-4567'     ] } 

My code so far (assume json_object is a dict at this point):

if not json_object.has_key('type'):     raise TypeError('JSON stream is missing a 'type' attribute.')  ##### THIS IS WHERE I WANT TO CHECK IF THE TYPE BY THAT NAME EXISTS ALREADY #### # create a type definition and add attributes to it definition = type(json_object['type'], (object,), {}) for key in json_object.keys():     setattr(definition, key, None)  # instantiate and populate a test object tester = definition() for key, value in json_object.iteritems():     setattr(tester, key, value) 
  • 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. 2026-05-10T22:26:38+00:00Added an answer on May 10, 2026 at 10:26 pm

    You can use dir() to get a list of all the names of all objects in the current environment, and you can use globals() to a get a dictionary mapping those names to their values. Thus, to get just the list of objects which are classes, you can do:

    import types listOfClasses = [cls for cls in globals().values() if type(cls) == types.ClassType] 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 222k
  • Answers 222k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just use parameterized queries! Check out this article here: http://www.functionx.com/aspnet/sqlserver/parameterized.htm May 13, 2026 at 12:17 am
  • Editorial Team
    Editorial Team added an answer try this: public static String fromStandartToExponent(String v){ return String.format("%e",Double.parseDouble(v)); }… May 13, 2026 at 12:17 am
  • Editorial Team
    Editorial Team added an answer Take a look at this answer -> File input 'accept'… May 13, 2026 at 12:17 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.