0

I can read the json file and set the information I need to the variables. I have accounts, and I need to be able to add an account to it, this is the code I have so far:

import json

user = raw_input("User: ")

with open('text.json') as data_file:
    data = json.load(data_file)

code =  data["users"][user]["pass"]
display = data["users"][user]["display"]

print "Password: " + code
print "Display Name - " + display

This works fine to read the file, here is the expected format of the output json file:

{
  "users":{
    "User1": {
      "display": "User1",
      "pass": "EzPass123"
    },
    "Richard": {
      "display": "Attack69",
      "pass": "Smith"
    }
  }
}

Can someone show me how to add another account to the accounts already existing in the dictionary?

4
  • give an example how your result is supposed to look like Commented Jan 17, 2016 at 20:07
  • I did in the json file, it is suppose to look like that Commented Jan 17, 2016 at 20:08
  • So? Use the dictionary as you normally would then dump after modification. Commented Jan 17, 2016 at 20:14
  • I am not familiar with the json lingo, the code I have is from another question, i just modified it. Can you explain in a little more detail please? Commented Jan 17, 2016 at 20:15

2 Answers 2

2
import json

# here you read your new user and his password
user = raw_input("User: ")
display = raw_input("Display name: ")
pwd = raw_input("Pass: ")

with open('text.json') as data_file:
    data = json.load(data_file)
    # update the data dictionary then re-dump it in the file
    data['users'].update({
        user: {
            'display': display,
            'pass': pwd
        }
    })

with open('text.json', 'w') as data_file:
    json.dumps(data, data_file, indent=4)
Sign up to request clarification or add additional context in comments.

11 Comments

that replaces user1 witht that information, I need to add a new user
whops, typoed, updating now
that doesn't add another account for some reason, but I have no errors
json.dump, not json.dumps. Probably need to rewind the file too if using the same open instance for reading and writing.
Traceback (most recent call last): File "add.py", line 17, in <module> json.dump(data, data_file) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 190, in dump fp.write(chunk)
|
0

Here's a solution covering the fixes discussed in the other answer:

#!python2
import json

# here you read your new user and his password
user = raw_input("User: ")
display = raw_input("Display name: ")
pwd = raw_input("Pass: ")

with open('text.json') as data_file:
    data = json.load(data_file)
    data['users'][user] = {'display':display,'pass':pwd}

with open('text.json','w') as data_file:
    json.dump(data, data_file, indent=4)

Output:

User: markt
Display name: Mark
Pass: 123

Result:

{
    "users": {
        "markt": {
            "display": "Mark",
            "pass": "123"
        },
        "Richard": {
            "display": "Attack69",
            "pass": "Smith"
        },
        "User1": {
            "display": "User1",
            "pass": "EzPass123"
        }
    }
}

7 Comments

Hey, Im making it where it users the username , password = data["users"][user]["pass"]
when making a new account, i check if there is an account with the name they enter, and if they enter a name not in the database, it breaks, is there a way to fix this?
is there a way i can set all the names to a variable, example here
@RichardSmith, you should ask a new question showing the additional code checking for an existing user.
data['users'].keys() will give you the names list.
|

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.