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

Swifter
  • 1
  • 2
  • Possible duplicate of [Python "extend" for a dictionary](https://stackoverflow.com/questions/577234/python-extend-for-a-dictionary) –  Apr 14 '19 at 02:49

2 Answers2

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)
furas
  • 134,197
  • 12
  • 106
  • 148
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"
}