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

The Archive Base Latest Questions

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

Trying to serialize a dict object into a json string using Python 2.7’s json

  • 0

Trying to serialize a dict object into a json string using Python 2.7’s json (ie: import json).

Example:
json.dumps({
    'property1':        'A normal string',
    'pickled_property': \u0002]qu0000U\u0012
})

The object has some byte strings in it that are “pickled” data using cPickle, so for json’s purposes, they are basically random byte strings. I was using django.utils’s simplejson and this worked fine. But I recently switched to Python 2.7 on google app engine and they don’t seem to have simplejson available anymore.

Now that I am using json, it throws an exception when it encounters bytes that aren’t part of UTF-8. The error that I’m getting is:

UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 0: invalid start byte

It would be nice if it printed out a string of the character codes like the debugging might do, ie: \u0002]q\u0000U\u001201. But I really don’t much care how it handles this data just as long as it doesn’t throw an exception and continues serializing the information that it does recognize.

How can I make this happen?

Thanks!

  • 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-10T23:49:44+00:00Added an answer on June 10, 2026 at 11:49 pm

    The JSON spec defines strings in terms of unicode characters. For this reason, the json module assumes that any str instance it receives holds encoded unicode text. It will try UTF-8 as its default encoding, which causes trouble when you have a string like the output of pickle.dumps which may not be a valid UTF-8 sequence.

    Fortunately, fixing the issue is easy. You simply need to tell the json.dumps function what encoding to use instead of UTF-8. The following will work, even though my_bytestring is not valid UTF-8 text:

    import json, cPickle as pickle
    
    my_data = ["some data", 1, 2, 3, 4]
    my_bytestring = pickle.dumps(my_data, pickle.HIGHEST_PROTOCOL)
    json_data = json.dumps(my_bytestring, encoding="latin-1")
    

    I believe that any 8-bit encoding will work in place of the latin-1 used here (just be sure to use the same one for decoding later).

    When you want to unpickle the JSON encoded data, you’ll need to make a call to unicode.decode, since json.loads always returns encoded strings as unicode instances. So, to get the my_data list back out of json_data above, you’d need this code:

    my_unicode_data = json.loads(json_data)
    my_new_bytestring = my_unicode_data.encode("latin-1")  # equal to my_bytestring
    my_new_data = pickle.loads(my_new_bytestring)          # equal to my_data
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to serialize a C# object to JSON using JSON.net library. The
I am trying to serialize a queryset into json using my custom iterator. On
I'm trying to serialize a list of python objects with JSON (using simplejson) and
I am trying to serialize a PagedList object ( https://github.com/martijnboland/MvcPaging/blob/master/src/MvcPaging/PagedList.cs ) to Json, like
I am trying to serialize/deserialize JSON in Android using GSON. I have two classes
I've been trying to serialize an XML (jQuery object) to string to POST it
I'm trying to serialize into a Xml File an object which is declared as
I am trying to serialize an object & save it into a Sql server
I have been trying to serialize some json data in Silverlight. I am using
I am trying to serialize a class to a string using the boost serialization

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.