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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:41:16+00:00 2026-06-09T02:41:16+00:00

I have a list, which may be empty or non-empty. I want to create

  • 0

I have a list, which may be empty or non-empty.

I want to create a new file which contains that list in a format that is human-readable and easy for my next script to parse. In the case where the list is non-empty, this works fine and my next script reads in the json file. But when the list is empty, I get “ValueError: No JSON object could be decoded.” This makes sense, because when I open the file, there is indeed no content and thus no JSON object.

I’m OK with the fact that some lists are empty. So, either I want to write an empty JSON object or I want my reader script to be OK with not finding a JSON object.

Here’s the relevant code:

Writer Script

favColor = []   OR   favColor = ['blue']   OR favColor = ['blue', 'green']
fileName = 'favoriteColor.json'
outFile = open(fileName, 'w')
json.dump(outFile, favColor)
outFile.close()

Reader Script

fileName = 'favoriteColor.json'
inFile = open(fileName, 'r')
colors = json.load(inFile)
inFile.close()

Any help or suggestions much appreciated. If I need to provide more rationale for why I’m doing this, I can provide that as well, just thought I’d start off with minimum necessary to understand the problem.

  • 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-09T02:41:18+00:00Added an answer on June 9, 2026 at 2:41 am

    Modify your reader script to this:

    with open('favoriteColor.json') as inFile:
        try: 
             colors = json.load(inFile)
        except ValueError: 
             colors = []
    

    This attempts to load the file as a json. If it fails due to a value error, we know that this is because the json is empty. Therefore we can just assign colors to an empty list. It is also preferable to use the “with” construct to load files since it closes them automatically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several lists which may or may not be empty. I want to
I have a list which contains numeric strings. The numeric strings in list may
I have a list, which may contain elements that will compare as equal. I
I have a list of html pages which may contain certain encoded characters. Some
I have an List with one element in it, but which may have more
I have a simple list of strings, which may be of arbitrary length. I'd
I have an std::list<TYPE> which may contain child classes of TYPE. I need to
I have a list of user identifiers which are pretty long. The identifiers may
I have a list which has repeating items and I want a list of
In Python I have a list of strings, some of which may be the

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.