Reputation: 183
I have a simple JSON
{
"users":[
{
"last result": "2%",
"login": "user",
"password": "1",
"id": "1"
}
]
}
I want to add new user and then delete user by ID.
for example:
i want to add this:
{"id":"2", "login": "admin", "password":"22", "last result": "10%"}
after my action, json should look like this:
{
"users":[
{
"last result": "2%",
"login": "1user",
"password": "1",
"id": "2"
},
{
"id":"2",
"login": "admin",
"password":"22",
"last result": "10%"
}
]
}
I've tried this:
...
user = {"id": 2, "login": "admin", "password": "1", "last result": "2%"}
def save_data_to_json(user):
jsonFile = open("auth.json", "r+")
data = json.load(jsonFile)
jsonFile.close()
data["users"] = user
jsonFile = open("auth.json", "a+")
jsonFile.write(json.dumps(data))
jsonFile.close()
But after this, my JSON looks a little bit strange:
{
"users":[
{
"last result": "2%",
"login": "1user",
"password": "1",
"id": "2"
}
]
}{"users": {"id": 2, "login": "1user", "password": "1", "last result": "2%"}}
Upvotes: 0
Views: 678
Reputation: 174624
Your json object is has a key 'users' which is a collection of user objects. In your code though, you are doing data['users'] = user
which is just replacing the existing collection with your new user, not adding to it:
>>> s = '''{
... "users":[
... {
... "last result": "2%",
... "login": "user",
... "password": "1",
... "id": "1"
... }
... ]
... }'''
>>> o = json.loads(s)
>>> o['users']
[{u'last result': u'2%', u'login': u'user', u'password': u'1', u'id': u'1'}]
>>> user = {"id": 2, "login": "admin", "password": "1", "last result": "2%"}
>>> o['users'] = user
>>> o['users']
{'last result': '2%', 'login': 'admin', 'password': '1', 'id': 2}
Note now how the object has changed. Instead of it being a collection of users, its now just one user object. This will have an impact on whatever application is reading this file.
Instead you need to do this:
>>> o['users'].append(user)
>>> o['users']
[{u'last result': u'2%', u'login': u'user', u'password': u'1', u'id': u'1'}, {'last result': '2%', 'login': 'admin', 'password': '1', 'id': 2}]
Now you have two users in the 'users' collection, to delete a user by their value:
>>> o['users'] = [u for u in o['users'] if u['login'] != 'admin'] # Get rid of the admin
>>> o['users']
[{u'last result': u'2%', u'login': u'user', u'password': u'1', u'id': u'1'}]
Then you can convert the object back to json and write it out.
Putting all this together, your code is:
with open('auth.json', 'r') as f:
data = json.load(f)
data['users'].append(user)
# If you want to delete
data['users'] = [u for u in o['users'] if u['login'] != 'admin'] # Get rid of the admin
# Write it back
with open('auth.json', 'w') as f:
json.dump(data, f)
Upvotes: 0
Reputation: 21243
Try this code
import json
user = {"id": 2, "login": "admin", "password": "1", "last result": "2%"}
def save_data_to_json(user):
jsonFile = open("/tmp/auth.json", "r+")
data = json.load(jsonFile)
jsonFile.close()
data["users"].append(user)
jsonFile = open("/tmp/auth.json", "w")
jsonFile.write(json.dumps(data))
jsonFile.close()
save_data_to_json(user)
Upvotes: 0
Reputation: 5412
You should open auth.json
with w
and not with a+
. When you open with a+
it means you append your output to it instead of overwriting it.
Upvotes: 2