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
  Capture json data JohnnyCoffee 0 262 Nov-18-2021, 03:19 PM
Last Post: JohnnyCoffee
  How to save json data in a dataframe shantanu97 1 872 Apr-15-2021, 02:44 PM
Last Post: klllmmm
  Sort data from JSON file Dummy_in_programming 2 1,028 Jan-04-2021, 06:17 PM
Last Post: deanhystad
  Sublime Text + Anaconda not indenting Python code correctly Maxximiliann 0 830 Jun-13-2020, 06:09 PM
Last Post: Maxximiliann
  Get Python json data i n SQL Server Djin 1 1,023 May-13-2020, 05:56 AM
Last Post: ndc85430
  difficulties to chage json data structure using json module in python Sibdar 1 1,053 Apr-03-2020, 06:47 PM
Last Post: micseydel
  Get json data JohnnyCoffee 0 865 Feb-20-2020, 06:14 PM
Last Post: JohnnyCoffee
  Regex to retrieve data from json in script tag. DreamingInsanity 4 6,579 Dec-20-2019, 06:18 PM
Last Post: DreamingInsanity
  extract specific data from a group of json-files ledgreve 3 1,747 Dec-05-2019, 07:57 PM
Last Post: ndc85430
  unable to indent json data dataplumber 4 1,380 Oct-22-2019, 01:55 PM
Last Post: dataplumber

Forum Jump:

User Panel Messages

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