Python Forum
Serializing Python data Correctly (JSON)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Serializing Python data Correctly (JSON)
#1
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")
Reply
#2
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.
JgKSuperstar likes this post
Reply
#3
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"
    } 
  }
]
Reply
#4
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.
JgKSuperstar likes this post
Reply
#5
(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:
* 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.

(Nov-04-2021, 06:22 PM)deanhystad Wrote: 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 data dictionaries 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.

Thank you very much. I also didn't know about the @dataclass decorator. I can learn about it in detail now :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why can I not see the code correctly in Python IDLE.? Trump 7 433 Mar-14-2024, 09:05 PM
Last Post: deanhystad
  Correctly read a malformed CSV file data klllmmm 2 1,812 Jan-25-2023, 04:12 PM
Last Post: klllmmm
  Read nested data from JSON - Getting an error marlonbown 5 1,309 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Reading Data from JSON tpolim008 2 1,028 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Python Split json into separate json based on node value CzarR 1 5,470 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  Convert nested sample json api data into csv in python shantanu97 3 2,724 May-21-2022, 01:30 PM
Last Post: deanhystad
  Struggling with Juggling JSON Data SamWatt 7 1,819 May-09-2022, 02:49 AM
Last Post: snippsat
  json api data parsing elvis 0 902 Apr-21-2022, 11:59 PM
Last Post: elvis
  Capture json data JohnnyCoffee 0 1,173 Nov-18-2021, 03:19 PM
Last Post: JohnnyCoffee
  How to save json data in a dataframe shantanu97 1 2,121 Apr-15-2021, 02:44 PM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020