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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:49:58+00:00 2026-06-13T23:49:58+00:00

What is a good way to prevent the python’s json library from throwing an

  • 0

What is a good way to prevent the python’s json library from throwing an exception when it encounters objects that it does not know how to serialize?

We are using json to serialize dict objects, sometimes properties of the objects are not recognized by the json library, causing it to throw an exception. Instead of throwing an exception, it would be nice if it just skipped over that property of the dict instead. It could set the property value to “None” or even a message: “Cannot serialize”.

Right now, the only way I know how to do this is to explicitly identify and skip over each data type that json might encounter that will make it throw an exception. As you can see, I have it turning datetime objects into strings, but also skipping over some geo-point objects from the shapely library:

import json
import datetime
from shapely.geometry.polygon import Polygon
from shapely.geometry.point import Point
from shapely.geometry.linestring import LineString

# This sublcass of json.JSONEncoder takes objects from the
# business layer of the application and encodes them properly
# in JSON.
class Custom_JSONEncoder(json.JSONEncoder):

    # Override the default method of the JSONEncoder class to:
    # - format datetimes using strftime('%Y-%m-%d %I:%M%p')
    # - de-Pickle any Pickled objects
    # - or just forward this call to the superclass if it is not
    #   a special case object
    def default(self, object, **kwargs):
        if isinstance(object, datetime.datetime):
            # Use the appropriate format for datetime
            return object.strftime('%Y-%m-%d %I:%M%p')
        elif isinstance(object, Polygon):
            return {}
        elif isinstance(object, Point):
            return {}
        elif isinstance(object, Point):
            return {}
        elif isinstance(object, LineString):
            return {}

        return super(Custom_JSONEncoder, self).default(object)
  • 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-06-13T23:49:59+00:00Added an answer on June 13, 2026 at 11:49 pm

    This should do what you want. You can add special cases before the catch-all return, or customize the fallback value.

    import json
    import datetime
    
    
    class Custom_JSONEncoder(json.JSONEncoder):
        def default(self, obj, **kwargs):
            if isinstance(obj, datetime.datetime):
                # Use the appropriate format for datetime
                return obj.strftime('%Y-%m-%d %I:%M%p')
            return None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out a good way to prevent developers from force pushing
Is there a good way to keep keys from conflicting when using the Microsoft
Good morning, I am currently writing a python library. At the moment, modules and
Is this a good way to prevent SQL injection before running a database query?
What is a good way to prevent a table with 2 columns, a (unique)
Is there a good way of preventing an activity from launching? I'd like to
I'm looking for a good and inexpensive way to prevent denial of service attacks
In this topic I found a good way to prevent cascade deleting of relating
From what I understand, Python lambda's only allow expressions, not statements. I have a
I have a few things that I cannot find a good way to perform

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.