1

Currently learning to use JSON data structures with python, have been able to read in files and write files but am having some issues with updating an existing file, getting the error: 'str' object has no attribute 'update'. I have searched and tried the solutions others have suggested but have had no luck in fixing this. Below is my code, the idea is that it adds a new user/password to the existing file.

import json

sUser_Login = {}
user = input('Create Username: >> ')
passw = input('Create Password: >> ')

sUser_Login[user] = passw


with open('JSONData.txt', 'r') as f:
    data = json.load(f)

data.update(sUser_Login)


with open('JSONData.txt', 'w')as f:
    json.dump(data, f)

Appreciate any assistance. Thanks Again

EDIT: Here is the data in the JSONData.txt file:

"{\"Jrob\": \"abc123\", \"jrobertson\": \"123abc\"}"
2
  • try something like , data[user] =passw (or) data.update({"user":"passw"}) Commented Mar 9, 2017 at 4:29
  • Please share your JSONDATA.txt ..... Commented Mar 9, 2017 at 4:38

1 Answer 1

1

Your code is actually fine, as long as you first create the file JSONData.txt containing just {}, an empty dictionary (or "object" in JSON terminology). It looks like when you are running it, JSONData.txt contains a JSON string? Then data ends up as a string, and a string doesn't have an update() method hence the error.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the swift reply, the file does contain a JSON string, is there a way to append to or update the data in the JSON file? For example the file currently has "{\"Jrob\": \"abc123\", \"jrobertson\": \"123abc\"}" and ideally i want it do add the new entry to be "{\"Jrob\": \"abc123\", \"jrobertson\": \"123abc\", \"Maxy\": \"password\"}"
{"Jrob": "abc123", "jrobertson": "123abc", "Maxy": "password"} is a valid JSON dictionary. "{\"Jrob\": \"abc123\", \"jrobertson\": \"123abc\", \"Maxy\": \"password\"}" is a JSON string.
@JoshuaRobertson Ah, your file should contain {"Jrob": "abc123", "jrobertson": "123abc"} and then your code will work fine. That is a JSON object, which will load as a dictionary like you want. The way your file is now, it just contains a single JSON string, and so it loads as a string. Let me know if you need more explanation.
Thanks again for the replies, Completely makes sense. I am guessing that when I wrote the file using python originally I used the incorrect method, how do I write the data so it saves as a JSON dictionary and not a JSON string?
@JoshuaRobertson The code you posted initially works fine when JSONData.txt starts out correctly. In other words, your current code is correctly saving it as a JSON dictionary. You probably got a messed up file because you double-JSON-encoded your dictionary. For example, try print(json.dumps(json.dumps({"Jrob": "abc123", "jrobertson": "123abc"}))) and you'll get that.

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.