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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:46:21+00:00 2026-05-24T15:46:21+00:00

[noob question] What’s the simplest way to encode a class into JSON in python

  • 0

[noob question]
What’s the simplest way to encode a class into JSON in python running on GAE? Not using Django and just whatever built-in stuff that GAE has.

My class is defined like this:

class Sample:
    def __init__(self, myName, myEmail, myLocality, myAliases, myRoles):
        self.name = myName;
        self.email = myEmail;
        self.locality = myLocality
        self.aliases = myAliases;        # this is a list of strings
        self.roles = myRoles             # this is a dictionary

I want to transform it so it looks like this in json:

{sample: 
    name: "a name value",
    email: "whatever email value",
    aliases: [alias1, alias2, alias3],
    locality: "some locality value",
    roles: {
        name: "some name value",
        type: "some type value",
        aux: "additional information"
        }
}

I plan to return this as the response data for the request.

Do I need to come up with a custom Encoder class for my ‘Sample’ class?

  • 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-24T15:46:22+00:00Added an answer on May 24, 2026 at 3:46 pm

    How about this:

    >>> import json
    >>> json.dumps(Sample(1, 2, [3, 4], 4, {5: 5, 5: 5}).__dict__)
    1: '{"aliases": 4, "locality": [3, 4], "name": 1, "roles": {"5": 5}, "email": 2}'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Noob question... class msgbox: def __init__(self, lbl_msg = '', dlg_title = ''): self.wTree =
Noob question. I have this situation where I have these objects: class Address {
Total noob question here, but not finding the answer via search. What is the
C# noob question... I'm taking a few arguments in a class constructor to initialize
Noob question (probably). I have a class with a var textFieldObjets:Textfield in my class
Noob question (because I'm a noob when it comes to Web development). I'm not
Total noob question on DotNetNuke, using it for first time. My client gave me
Noob question, using Win7 64-bit, Clojure 1.2.0, Java 1.6.0_22 When I start clojure from
(pardon the noob question in advance) I have 4 classes: class Person {}; class
noob question: I'm building a checkout system for an online commerce site. I'm getting

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.