1

I want to update my json file using a python script, however the json in the .json file is inside of an array [] it looks like this:

[
  {
    "test": "2",
    "test1": "1"
  },
  
  {
    "test2": "3",
    "test3": "4"
  }
]

So far I've tried:

username = "test"

with open("file.json", "a+") as f:
  json_file = json.load(f)
  json_file[0]['username'] = username
  json.dump(json_file, f)

error:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)```
6
  • The value you are trying to change is not in your .json you have provided. Commented Jul 23, 2020 at 1:25
  • How can I make it make another object inside of the array with username Commented Jul 23, 2020 at 1:25
  • Ok got you, so you want to add a new value to the json file Commented Jul 23, 2020 at 1:26
  • Yes with the values I give it (for e.x test and test1, both inside a new object inside the array Commented Jul 23, 2020 at 1:27
  • 1
    I think the problem is with the file mode a+. You're starting at the end of the file, and so json.load() has nothing to read. Commented Jul 23, 2020 at 1:33

2 Answers 2

1

As @JohnGordon commented, the error occurs on the json_file = json.load(f) line since the file pointer is at the end of file when the file is opened in a+ mode. You can see that when we move the file pointer to the beginning of the file, reading is done successful:

import json

username = "test"

with open("file.json", "a+") as f:
    f.seek(0)  # move file pointer to the beginning
    data = json.load(f)
    print(data)

But when you use a+ mode, "the file pointer is returned to the end of file before every write". So when you write to the file, the json data is appended to the end of file, not updating the whole content of the file. So to update your json file, it is more appropriate to use the code below:

import json

username = "test"

with open("file.json", "r") as f:
    data = json.load(f)

data[0]['username'] = username

with open("file.json", "w") as f:
    json.dump(data, f)
Sign up to request clarification or add additional context in comments.

6 Comments

How can I make it append a new object inside the array with {"username": "test"}, instead of overwriting one that already exists?
@fiji You can do data.append({'username': username}) instead of data[0]['username'] = username.
Then I get AttributeError: 'dict' object has no attribute 'append' when I change it to data[0].append({'username': "test"}) I get KeyError: 0
@fiji Did you use the json file on your original post?
@fiji I think maybe your json file contains an empty dict {}, not an empty array []. The error message AttributeError: 'dict' object has no attribute 'append' shows that the read data is a dict not a list.
|
1

Try separating the 2 actions of opening and closing the file. Like this:

with open("file.json") as obj:
    json_file = json.load(obj)

data = json_file[0]
data["username"] = "test"

with open("file.json") as obj:
   json.dump(data, obj)

1 Comment

That worked, however how do I make a new obj inside of the array? So everytime the code runs it adds/appends a new obj with "username": "test"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.