Serializing Python data Correctly (JSON) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Serializing Python data Correctly (JSON) (/thread-35455.html) |
Serializing Python data Correctly (JSON) - JgKSuperstar - Nov-04-2021 Hi, I want to store the users I created in the file named "asdsad". The first information is being processed correctly, but the second information is overwritten by the first information. I don't want this. I want the "json" data to be written at the bottom. Where am I making a mistake? AccInformation.json { "67368262694": { "T.C": "32424324", "Adı": "Super", "Soyadı": "UZUN", "E-Posta": "[email protected]", "Kullanıcı Adı": "jgkSUperstar", "Şifresi": "123456", "Kullanıcı Anahtarı": "KDW6-T7L1-EPMB-H92Y-AS0O-VNZ4", "Kayıt Tarihi": "04/Kasim/2021 - Saat : 15:14" } }The next data needs to come under the above data. Example : { "67368262694": { "T.C": "32424324", "Adı": "Super", "Soyadı": "UZUN", "E-Posta": "[email protected]", "Kullanıcı Adı": "jgkSUperstar", "Şifresi": "123456", "Kullanıcı Anahtarı": "KDW6-T7L1-EPMB-H92Y-AS0O-VNZ4", "Kayıt Tarihi": "04/Kasim/2021 - Saat : 15:14" }, "12457262694": { "T.C": "75424323", "Adı": "aaaaaa", "Soyadı": "KISA", "E-Posta": "[email protected]", "Kullanıcı Adı": "Harveyspecter", "Şifresi": "123456", "Kullanıcı Anahtarı": "CDW6-T7L1-EPMB-H92Y-AS0O-VNZ4", "Kayıt Tarihi": "04/Kasim/2021 - Saat : 15:14" } }I have changed my codes many times, but I have not been able to overcome this problem.. Mycode : import sqlite3 as sqlite import Generator import datetime import locale import GeneralSecure import os import json import time locale.setlocale(locale.LC_ALL, '') # Account Management class Account: def __init__(self, user_id, TCKN, firstName, lastName, email, username, password, accountKEY, register_date): #, Get & Set self.user_id = user_id self.TCKN = TCKN self.firstName = firstName self.lastName = lastName self.email = email self.username = username self.password = password self.accountKEY = accountKEY self.register_date = register_date class AccountManagement: def __init__(self): self.PendingUsers = {} def CreateAccount(self, user_id, TCKN, firstName, lastName, email, username, password, accountKEY, register_date, filename='AccInformation.json'): gay = datetime.datetime.now() # gay = gün / ay / yıl / saat register_date = datetime.datetime.strftime(gay, '%d/%B/%Y - Saat : %H:%M') igenerator = Generator.Production() user_id = igenerator.id_generator() accountKEY = igenerator.license_generator() self.PendingUsers.update({ user_id:{ "T.C": TCKN, "Adı": firstName, "Soyadı": lastName, "E-Posta": email, "Kullanıcı Adı": username, "Şifresi": password, "Kullanıcı Anahtarı": accountKEY, "Kayıt Tarihi": register_date } }) self.WriteFile(self.PendingUsers) def WriteFile(self, user, filename='AccInformation.json'): with open(filename, "r+", encoding='utf-8') as json_write: json.dump(user, json_write, indent=1, ensure_ascii=False) register = AccountManagement() register.CreateAccount("128", "32424324", "eeeee", "UZUN", "[email protected]", "test", "123456", "1", "1") RE: Serializing Python data Correctly (JSON) - bowlofred - Nov-04-2021 You can't append to JSON data like a log file or a database. If you want to add data to it you need to: * read in the existing data to a list or dict * append or add your data to that object * dump the json again (which has all the information). With a database you could just do the 'insert' and have it keep track of the old data. JSON can't do that. RE: Serializing Python data Correctly (JSON) - JgKSuperstar - Nov-04-2021 Yes, I know that, and here's how I did it ; def accountRegister(self, user: Account): self.users.append(user) self.saveToFile() def saveToFile(self): list = [] for user in self.users: list.append(user.__dict__) #print(list) with open('AccInformation.json', 'w', encoding='utf-8') as file: # We send the list and save it in the file. json.dump(list, file, indent=1)In this way, the list is constantly updated from top to bottom. So far, there are no problems. There's only one problem ; AccInformation.json [ { "user_id": 30694701697, "firstName": "Ahmet", "lastName": "Bilmemne", "email": "[email protected]", "username": "Ahmetce", "password": "03128232b0f696e4d13648373de38c5eef737fcb014b989d90bffa559b8424e8:51199754fd7647ddb15a8a992691027c", "accountKEY": "FYZA-J50V-XKBM-W9U1-RO4C-HNDE", "register_date": "Thu Nov 4 20:20:56 2021", "accessLevel": "Customer" }, { "user_id": 12875583752, "firstName": "Ahmet", "lastName": "Bilmemne", "email": "[email protected]", "username": "Ahmetce", "password": "4c768b6b5b054c11b8dc2d56bc0589ededf0f205bcf766eead77a3f3eff2e40d:c320138eeda743eda0c06d58f36323d3", "accountKEY": "PCE9-8NGQ-Z4FJ-1XLH-DW3V-K7UO", "register_date": "Thu Nov 4 20:20:57 2021", "accessLevel": "Customer" } ]How can I make the above data like this? Example.json [ { "30694701697":{ "firstName": "Ahmet", "lastName": "Bilmemne", "email": "[email protected]", "username": "Ahmetce", "password": "03128232b0f696e4d13648373de38c5eef737fcb014b989d90bffa559b8424e8:51199754fd7647ddb15a8a992691027c", "accountKEY": "FYZA-J50V-XKBM-W9U1-RO4C-HNDE", "register_date": "Thu Nov 4 20:20:56 2021", "accessLevel": "Customer" } }, { "12875583752" : { "firstName": "Ahmet", "lastName": "Bilmemne", "email": "[email protected]", "username": "Ahmetce", "password": "4c768b6b5b054c11b8dc2d56bc0589ededf0f205bcf766eead77a3f3eff2e40d:c320138eeda743eda0c06d58f36323d3", "accountKEY": "PCE9-8NGQ-Z4FJ-1XLH-DW3V-K7UO", "register_date": "Thu Nov 4 20:20:57 2021", "accessLevel": "Customer" } } ] RE: Serializing Python data Correctly (JSON) - deanhystad - Nov-04-2021 Instead of a user list you should have a user dictionary. This is a quick and ugly example: import sys import json from dataclasses import dataclass @dataclass class Account(): firstName:str lastName:str email:str username:str password:str class AccountManagement: def __init__(self): self.PendingUsers = {} def CreateAccount(self, user_id, firstName, lastName, email, username, password): self.PendingUsers[user_id] = Account(firstName, lastName, email, username, password) def WriteFile(self): users = {id: user.__dict__ for id, user in self.PendingUsers.items()} json.dump(users, sys.stdout) accounts = AccountManagement() accounts.CreateAccount(1, "a", "b", "c", "d", "e") accounts.CreateAccount(2, "A", "B", "C", "D", "E") accounts.WriteFile()I think there are packages that make dataclasses serializable. Using one of those is better than my __dict__ trick. You might also want to look at pydantic which makes a dataclass like thing that is serializeable to/from json. RE: Serializing Python data Correctly (JSON) - JgKSuperstar - Nov-04-2021 (Nov-04-2021, 04:24 PM)bowlofred Wrote: You can't append to JSON data like a log file or a database. If you want to add data to it you need to: (Nov-04-2021, 06:22 PM)deanhystad Wrote: Instead of a user list you should have a user dictionary. Thank you very much. I also didn't know about the @dataclass decorator. I can learn about it in detail now :) |