1

I need to be able to update a json file in python. I can get it to create and write to the json file the first time, but it keeps telling me I have an json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0).

Here's the code:

import pathlib
import json

obj = {
        "a": "1",
        "b": "2",
        "c": "3"
    }
obj2 = {
        "d": "4",
        "e": "5",
        "f": "6"
    }


jsonFile = pathlib.Path("jsonFile.json")
if jsonFile.exists() == False:
    with open("jsonFile.json", "w") as savefile:
        dumpJSON = json.dump(obj, savefile)
else:
    with open("jsonFile.json", "w+") as savefile:
        readObj = json.load(savefile)
        readObj.update(obj2)
        dumpJSON = json.dump(readObj, savefile)

Here's the full traceback.

Traceback (most recent call last):
  File "h:\Projects\Programs\Calculator\stackQuestion.py", line 22, in <module>
    readObj = json.load(savefile)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I'm using Python 3.9.2.
I've searched the following sites:
Read, Write, and Parse JSON using Python
Python-Difference Between json.load and json.loads
Append to JSON file using Python
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
How do I write JSON data to a file?
Python JSON AttributeError: 'str' object has no attribute 'read'

terminus
  • 23
  • 6
  • I don't know if this is the issue but the line `readObj = json.loads("createCache.json")` should be `readObj = json.load(savefile)`. – Daniel Walker Mar 23 '21 at 19:26
  • Please provide a **minimal** [mre] and get all that unrelated junk out of the code in your question. Also show the exact error message or, better yet, the whole traceback. – martineau Mar 23 '21 at 19:32
  • That fixed the first issue, but now I have another issue. `json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)` – terminus Mar 23 '21 at 19:34
  • I'll provide the minimal reproducible example as soon as I can. Give me a bit. – terminus Mar 23 '21 at 19:45
  • w+ mode truncates file, isn't it? – NobbyNobbs Mar 23 '21 at 20:02

2 Answers2

1

You cannot read and write the json file with the same with clause. Kindly use this code to get rid of that error that are the read and write buffers that not allowing you to write while reading at the same time

import pathlib
import json
import os

obj = {
        "a": "1",
        "b": "2",
        "c": "3"
    }
obj2 = {
        "d": "4",
        "e": "5",
        "f": "6"
    }


jsonFile = "jsonFile1.json"

print(obj)
if os.path.exists(jsonFile) == False:
    print("1123")
    with open(jsonFile, "w") as savefile:
        json.dump(obj, savefile)
else:
    with open(jsonFile, "r") as json_file:
        readObj = json.load(json_file)
        readObj.update(obj2)
        print(readObj)
        
        
    with open(jsonFile, "w") as json_file:
        json.dump(readObj, json_file)
Sohaib Anwaar
  • 1,517
  • 1
  • 12
  • 29
  • 1
    I'm a new contributor and my reputation is too low to upvote, so I'm leaving a comment to say thank you. You are a life saver. – terminus Mar 24 '21 at 11:23
0

Reading of json is answerd by Daniel. To write a json file use json.dump(jsonDict, outfile) to save a jsonString jsonvar = json.dumps(jsonDict) There are several parameters to use, take a look at the docs

Papageno
  • 305
  • 3
  • 15