0

I would like to append to an existing json array using python like this:

{
      "username": "user",
      "password": "password"
    }

from this to 
{
      "username": "user",
      "password": "password",
      "team name": "testTeam",
      "firstplayer": "firstPlayer",
      "secondplayer": "secondPlayer",
      "thirdplayer": "thirdPlayer",
      "fourth Player": "fourthPlayer",
      "fifthplayer": "fifthPlayer"
    }

this is my current python code

def createTeam():
    username = 'user'
    password = 'pass'
    teamName = 'testTeam' #input('What is your teams name? ')
    firstPlayer = 'firstPlayer'#input('Who is your first Player: ')
    secondPlayer = 'secondPlayer'#input('Who is your second Player: ')
    thirdPlayer = 'thirdPlayer'#input('Who is your third Player: ')
    fourthPlayer = 'fourthPlayer'#input('Who is your fourth Player: ')
    FifthPlayer = 'fifthPlayer'#input('Who is your fifth Player: ')

    f = open("users.json", "r")
    users = json.load(f)

    x = users['users'].append({'username': username, 'password': password, 'team name': teamName, 'firstplayer': firstPlayer, 'secondplayer': secondPlayer, 'thirdplayer': thirdPlayer, 'fourth Player': fourthPlayer, 'fifthplayer': FifthPlayer})


    with open('users.json', 'w') as f:
        json.dump(x, f, indent=2)

in short im just trying to update a record like in a database but i don't know how too

1

2 Answers 2

2

Python convert JSON to dictionary or list.

In your code it seems it is dictionary, not list - so it doesn't have append() - but I can't see full data from file to confirm this.

(BTW: append() doesn't create new list so you can't assing x = ...)

f = open("users.json", "r")
users = json.load(f)

users['users']['username'] = username
users['users']['password'] = password
# ... etc. ...

And now you can save users back (not x)

with open('users.json', 'w') as f:
    json.dump(users, f, indent=2)
Sign up to request clarification or add additional context in comments.

Comments

0

If you have

a = {
 "username": "user",
 "password": "password"
}

and

b = {
 "team name": "testTeam",
 "firstplayer": "firstPlayer",
 "secondplayer": "secondPlayer",
 "thirdplayer": "thirdPlayer",
 "fourth Player": "fourthPlayer",
 "fifthplayer": "fifthPlayer"
}

you can use

a.update(b)

to get

{
 "username": "user",
 "password": "password",
 "team name": "testTeam",
 "firstplayer": "firstPlayer",
 "secondplayer": "secondPlayer",
 "thirdplayer": "thirdPlayer",
 "fourth Player": "fourthPlayer",
 "fifthplayer": "fifthPlayer"
}

In your specific case, you may be able change your code to

users['users'].update({
    "team name": "testTeam",
    "firstplayer": "firstPlayer",
    "secondplayer": "secondPlayer",
    "thirdplayer": "thirdPlayer",
    "fourth Player": "fourthPlayer",
    "fifthplayer": "fifthPlayer"
}

However, if you are actually trying to update an existing user in the dictionary, you may need something like this

users['users'][username].update({
 "team name": "testTeam",
 "firstplayer": "firstPlayer",
 "secondplayer": "secondPlayer",
 "thirdplayer": "thirdPlayer",
 "fourth Player": "fourthPlayer",
 "fifthplayer": "fifthPlayer"
})

or if you are trying to add a user

users['users'][username] = {
 "username": "user",
 "password": "password",
 "team name": "testTeam",
 "firstplayer": "firstPlayer",
 "secondplayer": "secondPlayer",
 "thirdplayer": "thirdPlayer",
 "fourth Player": "fourthPlayer",
 "fifthplayer": "fifthPlayer"
}

Comments

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.