Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

python convert json string to class

class Person():
    def __init__(self, name, age):
        self.name = name
        self.age = age

person_string = '{"name": "Bob", "age": 25}'

person_dict = json.loads(person_string)
person_object = Person(**person_dict)

print(person_object)
Comment

python convert json string to module object class

import json
from collections import namedtuple
from json import JSONEncoder

def customStudentDecoder(studentDict):
    return namedtuple('X', studentDict.keys())(*studentDict.values())

#Assume you received this JSON response
studentJsonData = '{"rollNumber": 1, "name": "Emma"}'

# Parse JSON into an object with attributes corresponding to dict keys.
student = json.loads(studentJsonData, object_hook=customStudentDecoder)

print("After Converting JSON Data into Custom Python Object")
print(student.rollNumber, student.name)
Comment

python convert json string to module object class

import json
from collections import namedtuple
from json import JSONEncoder

class Student:
    def __init__(self, rollNumber, name, marks):
        self.rollNumber, self.name, self.marks = rollNumber, name, marks

class Marks:
    def __init__(self, english, geometry):
        self.english, self.geometry = english, geometry

class StudentEncoder(JSONEncoder):
        def default(self, o):
            return o.__dict__

def customStudentDecoder(studentDict):
    return namedtuple('X', studentDict.keys())(*studentDict.values())

marks = Marks(82, 74)
student = Student(1, "Emma", marks)

# dumps() produces JSON in native str format. if you want to writ it in file use dump()
studentJson = json.dumps(student, indent=4, cls=StudentEncoder)
print("Student JSON")
print(studentJson)

# Parse JSON into an object with attributes corresponding to dict keys.
studObj = json.loads(studentJson, object_hook=customStudentDecoder)

print("After Converting JSON Data into Custom Python Object")
print(studObj.rollNumber, studObj.name, studObj.marks.english, studObj.marks.geometry)
Comment

PREVIOUS NEXT
Code Example
Shell :: siege add autorizartion 
Shell :: uninstall requirements.txt 
Shell :: how to generate a resolver in the cli 
Shell :: CPU usage alert generator bash script 
Shell :: networkmanager runit 
Shell :: Background and Foreground Jobs 
Shell :: dart vlc flutter 
Shell :: check salt version 
Shell :: how to automatically change wall paper in kubuntu 
Shell :: service logs realtime 
Shell :: change drive in linux terminal windows subsystem 
Shell :: git clone with new name 
Shell :: bash if else regex 
Shell :: cd back 
Shell :: kubeadm get discovery-token-ca-cert-hash 
Shell :: fatal error: hiredis.h: aucun fichier ou dossier de ce type 
Shell :: stop running pre-commit hook: lint-staged 
Shell :: instalar arquivo .jar linux 
Shell :: py pip install error winerror the system cannot find the .exe.deleteme 
Shell :: git The requested URL returned error: 403 
Shell :: see unimported react comp. 
Shell :: linux cli create slideshow 
Shell :: how to safely clean snaps folder in ubuntu 
Shell :: linux show external drives 
Shell :: gitlab ssh two factorn recovery 
Shell :: pwsh exit script 
Shell :: sftp client online linux remote 
Shell :: Media change: please insert the disc 
Shell :: vim mass replace pattern across multiple files 
Shell :: erc20 token openzeppelin example github 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =