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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:54:07+00:00 2026-05-30T03:54:07+00:00

i have encountered a very strange issue: i use json.dump to write a file

  • 0

i have encountered a very strange issue: i use json.dump to write a file and then use json.load to read the file.

The same code can run succeed on windows 7 but it can not do on mac os x 10.7
Below is the code:

class Result:
def __init__(self,name,result):
    self.name = name
    self.result = result

def __repr__(self):
    return 'Result  name : %s , result : %s' % (self.name,self.result)

class MyEncoder(json.JSONEncoder):
def default(self,obj):
    #convert object to a dict
    d = {'CaseResult':{}}
    d['CaseResult'][obj.name] = obj.result
    return d


def save(name,result):
    filename = 'basic.json'
    obj = Result(name,result)
    obj_json = MyEncoder().encode(obj)
    with open(filename, mode='ab+') as fp:
        json.dump(obj_json,fp)
        s=json.load(fp)

save('aaa','bbb')

in mac os it give an error “ValueError:NO JSON object could be decoded”
who can tell me why this happen and how can i resolve it

  • 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-30T03:54:09+00:00Added an answer on May 30, 2026 at 3:54 am

    This problem is unrelated to being run on a Mac; this code should never work:

    with open(filename, mode='ab+') as fp:
        json.dump(obj_json,fp)
        s=json.load(fp)
    

    This is because after json.dump, your file pointer is at the end of the file. You must call fp.seek to reset it to the initial position, like this:

    import os
    with open(filename, mode='rb+') as fp:
        fp.seek(0, os.SEEK_END)
        pos = fp.tell()
        json.dump(obj_json,fp)
        fp.seek(pos)
        s=json.load(fp)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have encountered a very strange issue. Whenever I use the .NET membership provider
I have encountered a very strange problem in using jQuery load() . I used
We have encountered a very strange class not found problem in our web app
I have just encountered very strange problem - my GWT app hosted on Google
I have encountered something very strange, simple WPF application <Window x:Class=ListBoxSelection.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow
I have encountered a very strange thing today : I have an app installed
I have encountered a very strange bug in Firefox. I have a javascript function
I have encountered a very strange problem. The jquery slideshow in the below webpage
I have encountered a strange error. I use Windows 7 x64. Visual Studio 2010
I am having a strange issue that I have never encountered in the past

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.